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


The following commit(s) were added to refs/heads/master by this push:
     new acf2318eae8 fs/vfs: bump ctime in the common layer on metadata change
acf2318eae8 is described below

commit acf2318eae85e4020bd22484c4b7c5521c3437ed
Author: v-liuchenxu3 <[email protected]>
AuthorDate: Fri Nov 13 17:41:07 2026 +0800

    fs/vfs: bump ctime in the common layer on metadata change
    
    Changing file metadata (mode, owner, group, atime or mtime) is a POSIX
    status change that must bump ctime. This rule was re-implemented
    separately in each filesystem: tmpfs derived it from the chstat flags,
    and littlefs persisted st_ctim unconditionally on every fchstat/chstat.
    
    Add CH_STAT_CTIME and set it centrally in file_fchstat() and chstat()
    whenever a metadata field is actually changed, after the UTIME_OMIT
    filtering so a no-op utimens() does not spuriously bump ctime.
    buf->st_ctim already holds the current time, so filesystems only need
    to honor the flag.
    
    Drop the ctime derivation from tmpfs, and gate the littlefs ctim write
    on CH_STAT_CTIME (which also fixes littlefs bumping ctime on a no-op
    utimens). Proxy filesystems (hostfs/nfs/v9fs/rpmsgfs) already ignore
    CH_STAT_CTIME, so forwarding the flag is harmless.
    
    Assisted-by: Kiro:claude-opus-5.5
    Signed-off-by: v-liuchenxu3 <[email protected]>
---
 fs/littlefs/lfs_vfs.c | 14 ++++++++++----
 fs/tmpfs/fs_tmpfs.c   |  7 +------
 fs/vfs/fs_chstat.c    | 12 ++++++++++++
 fs/vfs/fs_fchstat.c   | 12 ++++++++++++
 include/nuttx/fs/fs.h |  1 +
 5 files changed, 36 insertions(+), 10 deletions(-)

diff --git a/fs/littlefs/lfs_vfs.c b/fs/littlefs/lfs_vfs.c
index ced608174ff..94f09de9271 100644
--- a/fs/littlefs/lfs_vfs.c
+++ b/fs/littlefs/lfs_vfs.c
@@ -1138,8 +1138,11 @@ static int littlefs_fchstat(FAR const struct file *filep,
       attr.at_gid = buf->st_gid;
     }
 
-  attr.at_ctim = 1000000000ull * buf->st_ctim.tv_sec +
-                 buf->st_ctim.tv_nsec;
+  if ((CH_STAT_CTIME & flags) == CH_STAT_CTIME)
+    {
+      attr.at_ctim = 1000000000ull * buf->st_ctim.tv_sec +
+                     buf->st_ctim.tv_nsec;
+    }
 
   if ((CH_STAT_ATIME & flags) == CH_STAT_ATIME)
     {
@@ -2189,8 +2192,11 @@ static int littlefs_chstat(FAR struct inode *mountpt,
       attr.at_gid = buf->st_gid;
     }
 
-  attr.at_ctim = 1000000000ull * buf->st_ctim.tv_sec +
-                 buf->st_ctim.tv_nsec;
+  if ((CH_STAT_CTIME & flags) == CH_STAT_CTIME)
+    {
+      attr.at_ctim = 1000000000ull * buf->st_ctim.tv_sec +
+                     buf->st_ctim.tv_nsec;
+    }
 
   if ((CH_STAT_ATIME & flags) == CH_STAT_ATIME)
     {
diff --git a/fs/tmpfs/fs_tmpfs.c b/fs/tmpfs/fs_tmpfs.c
index a40c7119363..c8409f343bc 100644
--- a/fs/tmpfs/fs_tmpfs.c
+++ b/fs/tmpfs/fs_tmpfs.c
@@ -885,12 +885,7 @@ static int tmpfs_chstat_object(FAR struct tmpfs_object_s 
*to,
       to->to_mtime = buf->st_mtim;
     }
 
-  /* Any metadata change is a status change.  The VFS has already filled
-   * st_ctim with the current time.
-   */
-
-  if ((flags & (CH_STAT_MODE | CH_STAT_UID | CH_STAT_GID |
-                CH_STAT_ATIME | CH_STAT_MTIME)) != 0)
+  if ((flags & CH_STAT_CTIME) != 0)
     {
       to->to_ctime = buf->st_ctim;
     }
diff --git a/fs/vfs/fs_chstat.c b/fs/vfs/fs_chstat.c
index a58e8b3c78e..6ae43c3b64f 100644
--- a/fs/vfs/fs_chstat.c
+++ b/fs/vfs/fs_chstat.c
@@ -182,6 +182,18 @@ static int chstat(FAR const char *path,
         }
     }
 
+  /* Changing the mode, owner, group or times is a status change, so mark
+   * st_ctime for update.  st_ctim already holds the current time.  This
+   * runs after the UTIME_OMIT handling, so a no-op utimens() leaves
+   * st_ctime alone.
+   */
+
+  if (flags & (CH_STAT_MODE | CH_STAT_UID | CH_STAT_GID |
+               CH_STAT_ATIME | CH_STAT_MTIME))
+    {
+      flags |= CH_STAT_CTIME;
+    }
+
   /* Perform the chstat operation */
 
   ret = chstat_recursive(path, buf, flags, resolve);
diff --git a/fs/vfs/fs_fchstat.c b/fs/vfs/fs_fchstat.c
index 2810cade41a..4aabeb9a24f 100644
--- a/fs/vfs/fs_fchstat.c
+++ b/fs/vfs/fs_fchstat.c
@@ -160,6 +160,18 @@ int file_fchstat(FAR struct file *filep, FAR struct stat 
*buf, int flags)
         }
     }
 
+  /* Changing the mode, owner, group or times is a status change, so mark
+   * st_ctime for update.  st_ctim already holds the current time.  This
+   * runs after the UTIME_OMIT handling, so a no-op utimens() leaves
+   * st_ctime alone.
+   */
+
+  if (flags & (CH_STAT_MODE | CH_STAT_UID | CH_STAT_GID |
+               CH_STAT_ATIME | CH_STAT_MTIME))
+    {
+      flags |= CH_STAT_CTIME;
+    }
+
   /* The way we handle the chstat depends on the type of inode that we
    * are dealing with.
    */
diff --git a/include/nuttx/fs/fs.h b/include/nuttx/fs/fs.h
index 348498ce3a4..9b0ada54d9d 100644
--- a/include/nuttx/fs/fs.h
+++ b/include/nuttx/fs/fs.h
@@ -178,6 +178,7 @@
 #define CH_STAT_GID        (1 << 2)
 #define CH_STAT_ATIME      (1 << 3)
 #define CH_STAT_MTIME      (1 << 4)
+#define CH_STAT_CTIME      (1 << 5)
 #define CH_STAT_SIZE       (1 << 7)
 #define CH_STAT_NOFOLLOW   (1 << 8)  /* Do not follow a final symbolic link */
 

Reply via email to