Hi Heinrich,

On 2026-10-12T14:00:39, Heinrich Schuchardt
<[email protected]> wrote:
> fs: ext4: set inode timestamps on write
>
> Replace the hardcoded zero timestamp in ext4fs_write() with the actual
> current time obtained from the RTC when CONFIG_DM_RTC is enabled.
>
> Per the ext2/3/4 specification (ext2 design document, section 4.2):
>   i_mtime  last data modification time
>   i_ctime  last inode change time (the 'c' stands for 'change', not
>            'create'; this is not a creation timestamp)
>   i_atime  last access time
>
> All three fields are set to the same value since writing data modifies
> both the file content (mtime) and the inode metadata (ctime), and any
> write also constitutes an access (atime).  If no RTC is available, the
> timestamp falls back to 2000-01-01 00:00:00 UTC, matching the FAT
> write driver behaviour.
>
> The ext4 i_crtime field (true file creation time, added in ext4) is
> stored in the extra inode area beyond the base 128-byte inode and is
> not modelled by struct ext2_inode, so it cannot be set here.
> [...]
>
> fs/ext4/ext4_write.c | 45 +++++++++++++++++++++++++++++++++++++++++++--
>  1 file changed, 43 insertions(+), 2 deletions(-)

> diff --git a/fs/ext4/ext4_write.c b/fs/ext4/ext4_write.c
> @@ -21,14 +21,47 @@
> +/**
> + * ext4_current_timestamp() - get current time as a POSIX timestamp
> + *
> + * Returns the current time as seconds since the Unix epoch.
> + * Falls back to 2000-01-01 00:00:00 UTC if the RTC is unavailable,
> + * matching the behaviour of the FAT write driver.
> + */
> +static time_t ext4_current_timestamp(void)
> +{
> +     if (CONFIG_IS_ENABLED(DM_RTC)) {
> +             struct udevice *dev;
> +             struct rtc_time tm;
> +
> +             uclass_first_device(UCLASS_RTC, &dev);
> +             if (!dev)
> +                     goto fallback;
> +             if (dm_rtc_get(dev, &tm))
> +                     goto fallback;
> +             return rtc_mktime(&tm);
> +     }
> +fallback:
> +     return EXT4_TIMESTAMP_Y2K;
> +}

This copies the RTC lookup from dentry_set_time() in fat_write.c, and
patch 6 adds a third copy in test/dm/host.c. I suggest a small common
helper, e.g. in rtc-uclass.c, that returns the current time in seconds
(or -ENODEV). FAT could convert that to its date/time format and ext4
could use it directly. Then the Y2K fallback, and the RTC
memory-allocation issue that 6/9 works around, are handled in one
place, and the next filesystem that wants timestamps can use the
helper too. What do you think?

For kernel-doc, please use a 'Return:' section rather than 'Returns'
in the body.

> diff --git a/fs/ext4/ext4_write.c b/fs/ext4/ext4_write.c
> @@ -979,7 +1012,15 @@ int ext4fs_write(const char *fname, const char *buffer,
> -     /* ToDo: Update correct time */
> +     /*
> +      * mtime: last data modification time (ext2/3/4 spec 4.2)
> +      * ctime: last inode change time - NOT creation time despite the name
> +      * atime: last access time - set to now since we are writing data
> +      *
> +      * Note: ext4 i_crtime (true creation time) lives in the extended
> +      * inode area beyond the base 128-byte inode and is not modelled
> +      * by struct ext2_inode, so it cannot be set here.
> +      */

ext4fs_update_parent_dentry() changes the parent directory but does
not update its mtime/ctime. That could be a follow-up, but please can
you mention it in the commit message so it is clear that only the file
inode is handled?

Regards,
Simon

Reply via email to