Skip to contentSkip to conversation

fat: split fat_truncate_time() into separate functions

PATCH Superseded Chung-Chiang Cheng 3 participants 7 messages Apr 23, 2022 – Apr 30, 2022

Subsystems (2)

v4 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

PATCH v4 1/3 fat: split fat_truncate_time() into separate functions

Separate fat_truncate_time() to each timestamps for later creation time work.

This patch does not introduce any functional changes, it's merely refactoring change.

---
 fs/fat/fat.h  |  6 +++++
 fs/fat/misc.c | 72 ++++++++++++++++++++++++++++++++-------------------
 2 files changed, 52 insertions(+), 26 deletions(-)
fs/fat/fat.h +6 -0
index 02d4d4234956..508b4f2a1ffb 100644
@@ -446,6 +446,12 @@ extern void fat_time_fat2unix(struct msdos_sb_info *sbi, struct timespec64 *ts,
 			      __le16 __time, __le16 __date, u8 time_cs);
 extern void fat_time_unix2fat(struct msdos_sb_info *sbi, struct timespec64 *ts,
 			      __le16 *time, __le16 *date, u8 *time_cs);
+extern void fat_truncate_atime(struct msdos_sb_info *sbi, struct timespec64 *ts,
+			       struct timespec64 *atime);
+extern void fat_truncate_crtime(struct msdos_sb_info *sbi, struct timespec64 *ts,
+				struct timespec64 *crtime);
+extern void fat_truncate_mtime(struct msdos_sb_info *sbi, struct timespec64 *ts,
+			       struct timespec64 *mtime);
 extern int fat_truncate_time(struct inode *inode, struct timespec64 *now,
 			     int flags);
 extern int fat_update_time(struct inode *inode, struct timespec64 *now,
fs/fat/misc.c +46 -26

87 lines not on this page · raw message

Signature · 1 line
2.34.1
#1 lore raw

PATCH v4 2/3 fat: ignore ctime updates, and keep ctime identical to mtime in memory

FAT supports creation time but not change time, and there was no corresponding timestamp for creation time in previous VFS. The original implementation took the compromise of saving the in-memory change time into the on-disk creation time field, but this would lead to compatibility issues with non-linux systems.

To address this issue, this patch changes the behavior of ctime. It will no longer be loaded and stored from the creation time on disk. Instead of that, it'll be consistent with the in-memory mtime and share the same on-disk field. All updates to mtime will also be applied to ctime in memory, while all updates to ctime will be ignored.

---
 fs/fat/inode.c | 11 ++++-------
 fs/fat/misc.c  | 12 +++++++++---
 2 files changed, 13 insertions(+), 10 deletions(-)
fs/fat/inode.c +4 -7
index bf6051bdf1d1..f2ac55cd4ea4 100644
@@ -567,12 +567,11 @@ int fat_fill_inode(struct inode *inode, struct msdos_dir_entry *de)
 			   & ~((loff_t)sbi->cluster_size - 1)) >> 9;
 
 	fat_time_fat2unix(sbi, &inode->i_mtime, de->time, de->date, 0);
-	if (sbi->options.isvfat) {
-		fat_time_fat2unix(sbi, &inode->i_ctime, de->ctime,
-				  de->cdate, de->ctime_cs);
+	inode->i_ctime = inode->i_mtime;
+	if (sbi->options.isvfat)
 		fat_time_fat2unix(sbi, &inode->i_atime, 0, de->adate, 0);
-	} else
-		fat_truncate_time(inode, &inode->i_mtime, S_ATIME|S_CTIME);
+	else
+		fat_truncate_atime(sbi, &inode->i_mtime, &inode->i_atime);
 
 	return 0;
 }
@@ -888,8 +887,6 @@ static int __fat_write_inode(struct inode *inode, int wait)
 			  &raw_entry->date, NULL);
 	if (sbi->options.isvfat) {
 		__le16 atime;
-		fat_time_unix2fat(sbi, &inode->i_ctime, &raw_entry->ctime,
-				  &raw_entry->cdate, &raw_entry->ctime_cs);
 		fat_time_unix2fat(sbi, &inode->i_atime, &atime,
 				  &raw_entry->adate, NULL);
 	}
fs/fat/misc.c +9 -3
index c87df64f8b2b..ef09b6361602 100644
@@ -341,10 +341,16 @@ int fat_truncate_time(struct inode *inode, struct timespec64 *now, int flags)
 
 	if (flags & S_ATIME)
 		fat_truncate_atime(sbi, now, &inode->i_atime);
-	if (flags & S_CTIME)
-		fat_truncate_crtime(sbi, now, &inode->i_ctime);
-	if (flags & S_MTIME)
+
+	/*
+	 * ctime and mtime share the same on-disk field, and should be
+	 * identical in memory. all mtime updates will be applied to ctime,
+	 * but ctime updates are ignored.
+	 */
+	if (flags & S_MTIME) {
 		fat_truncate_mtime(sbi, now, &inode->i_mtime);
+		inode->i_ctime = inode->i_mtime;
+	}
 
 	return 0;
 }
Signature · 1 line
2.34.1
#2 lore raw

PATCH v4 3/3 fat: report creation time in statx

In reply to Chung-Chiang Cheng

creation time is no longer mixed with change time. Add an in-memory field for it, and report it in statx if supported.

The unused code in fat_truncate_crtime() is also removed because only vfat support creation time.

---
 fs/fat/fat.h        |  1 +
 fs/fat/file.c       | 14 +++++++++++---
 fs/fat/inode.c      | 10 ++++++++--
 fs/fat/misc.c       |  3 +--
 fs/fat/namei_vfat.c |  1 +
 5 files changed, 22 insertions(+), 7 deletions(-)
fs/fat/fat.h +1 -0
index 508b4f2a1ffb..e4409ee82ea9 100644
@@ -126,6 +126,7 @@ struct msdos_inode_info {
 	struct hlist_node i_fat_hash;	/* hash by i_location */
 	struct hlist_node i_dir_hash;	/* hash by i_logstart */
 	struct rw_semaphore truncate_lock; /* protect bmap against truncate */
+	struct timespec64 i_crtime;	/* File creation (birth) time */
 	struct inode vfs_inode;
 };
 
fs/fat/file.c +11 -3
index a5a309fcc7fa..8f5218450a3a 100644
@@ -399,13 +399,21 @@ int fat_getattr(struct user_namespace *mnt_userns, const struct path *path,
 		struct kstat *stat, u32 request_mask, unsigned int flags)
 {
 	struct inode *inode = d_inode(path->dentry);
+	struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
+
 	generic_fillattr(mnt_userns, inode, stat);
-	stat->blksize = MSDOS_SB(inode->i_sb)->cluster_size;
+	stat->blksize = sbi->cluster_size;
 
-	if (MSDOS_SB(inode->i_sb)->options.nfs == FAT_NFS_NOSTALE_RO) {
+	if (sbi->options.nfs == FAT_NFS_NOSTALE_RO) {
 		/* Use i_pos for ino. This is used as fileid of nfs. */
-		stat->ino = fat_i_pos_read(MSDOS_SB(inode->i_sb), inode);
+		stat->ino = fat_i_pos_read(sbi, inode);
 	}
+
+	if (sbi->options.isvfat && request_mask & STATX_BTIME) {
+		stat->result_mask |= STATX_BTIME;
+		stat->btime = MSDOS_I(inode)->i_crtime;
+	}
+
 	return 0;
 }
 EXPORT_SYMBOL_GPL(fat_getattr);
fs/fat/inode.c +8 -2
index f2ac55cd4ea4..ebc124f44e86 100644
@@ -568,9 +568,11 @@ int fat_fill_inode(struct inode *inode, struct msdos_dir_entry *de)
 
 	fat_time_fat2unix(sbi, &inode->i_mtime, de->time, de->date, 0);
 	inode->i_ctime = inode->i_mtime;
-	if (sbi->options.isvfat)
+	if (sbi->options.isvfat) {
 		fat_time_fat2unix(sbi, &inode->i_atime, 0, de->adate, 0);
-	else
+		fat_time_fat2unix(sbi, &MSDOS_I(inode)->i_crtime, de->ctime,
+				  de->cdate, de->ctime_cs);
+	} else
 		fat_truncate_atime(sbi, &inode->i_mtime, &inode->i_atime);
 
 	return 0;
@@ -756,6 +758,8 @@ static struct inode *fat_alloc_inode(struct super_block *sb)
 	ei->i_logstart = 0;
 	ei->i_attrs = 0;
 	ei->i_pos = 0;
+	ei->i_crtime.tv_sec = 0;
+	ei->i_crtime.tv_nsec = 0;
 
 	return &ei->vfs_inode;
 }
@@ -887,6 +891,8 @@ static int __fat_write_inode(struct inode *inode, int wait)
 			  &raw_entry->date, NULL);
 	if (sbi->options.isvfat) {
 		__le16 atime;
+		fat_time_unix2fat(sbi, &MSDOS_I(inode)->i_crtime, &raw_entry->ctime,
+				  &raw_entry->cdate, &raw_entry->ctime_cs);
 		fat_time_unix2fat(sbi, &inode->i_atime, &atime,
 				  &raw_entry->adate, NULL);
 	}
fs/fat/misc.c +1 -2
index ef09b6361602..dfc5d6df3519 100644
@@ -309,9 +309,8 @@ void fat_truncate_crtime(struct msdos_sb_info *sbi, struct timespec64 *ts,
 {
 	if (sbi->options.isvfat)
 		*crtime = fat_timespec64_trunc_10ms(*ts);
-	else
-		*crtime = fat_timespec64_trunc_2secs(*ts);
 }
+EXPORT_SYMBOL_GPL(fat_truncate_crtime);
 
 /*
  * truncate mtime to 2 second granularity
fs/fat/namei_vfat.c +1 -0
index 5369d82e0bfb..9187979fed5d 100644
@@ -781,6 +781,7 @@ static int vfat_create(struct user_namespace *mnt_userns, struct inode *dir,
 	}
 	inode_inc_iversion(inode);
 	fat_truncate_time(inode, &ts, S_ATIME|S_CTIME|S_MTIME);
+	fat_truncate_crtime(MSDOS_SB(sb), &MSDOS_I(inode)->i_crtime, &MSDOS_I(inode)->i_crtime);
 	/* timestamp is already written, so mark_inode_dirty() is unneeded. */
 
 	d_instantiate(dentry, inode);
Signature · 1 line
2.34.1
maintainer
#3 lore raw

Re: [PATCH v4 3/3] fat: report creation time in statx

fs/fat/namei_vfat.c static int vfat_create(struct user_namespace *mnt_userns, struct inode *dir, · line 787

Quoted 12 lines · Chung-Chiang Cheng
fs/fat/namei_vfat.c +1 -0
index 5369d82e0bfb..9187979fed5d 100644
@@ -781,6 +781,7 @@ static int vfat_create(struct user_namespace *mnt_userns, struct inode *dir,
 	}
 	inode_inc_iversion(inode);
 	fat_truncate_time(inode, &ts, S_ATIME|S_CTIME|S_MTIME);
+	fat_truncate_crtime(MSDOS_SB(sb), &MSDOS_I(inode)->i_crtime, &MSDOS_I(inode)->i_crtime);
 	/* timestamp is already written, so mark_inode_dirty() is unneeded. */
 
 	d_instantiate(dentry, inode);

Probably, above should be the follow line?

	fat_truncate_crtime(MSDOS_SB(sb), &ts, &MSDOS_I(inode)->i_crtime);

And furthermore, this is missing to add it to mkdir(2)? And another one, we would have to update vfat_build_slots() for crtime? I'm not checking fully though, this seems to need isvfat test

	fat_time_unix2fat(sbi, ts, &time, &date, &time_cs);
	de->time = de->ctime = time;
	de->date = de->cdate = de->adate = date;
	de->ctime_cs = time_cs;

Thanks.

Signature · 1 line
OGAWA Hirofumi <[email protected]>
#4 lore raw

Re: [PATCH v4 3/3] fat: report creation time in statx

Quoted 4 lines · OGAWA Hirofumi

Probably, above should be the follow line?

        fat_truncate_crtime(MSDOS_SB(sb), &ts, &MSDOS_I(inode)->i_crtime);
Quoted 8 lines

And furthermore, this is missing to add it to mkdir(2)? And another one, we would have to update vfat_build_slots() for crtime? I'm not checking fully though, this seems to need isvfat test

        fat_time_unix2fat(sbi, ts, &time, &date, &time_cs);
        de->time = de->ctime = time;
        de->date = de->cdate = de->adate = date;
        de->ctime_cs = time_cs;

Ah, you're right. I missed vfat_mkdir() in the previous patch. After further study, I found that maybe we can remove fat_truncate_crtime() in both vfat_create() and vfat_mkdir(). crtime will come from only these two cases:

(1) read from disk
(2) generated in vfat_build_slots()

They are all in {cdate:16, ctime:16, ctime_cs:8} format, which ensures crtime will be kept at the correct granularity (10 ms). The remaining timestamps may be copied from the vfs inode, so we need to truncate them to fit FAT's format. But crtime doesn't need to do that.

Thanks.

maintainer
#5 lore raw
In reply to Chung-Chiang Cheng
Quoted 5 lines · Chung-Chiang Cheng
fs/fat/misc.c +5 -0
+/*
+ * truncate atime to 24 hour granularity (00:00:00 in local timezone)
+ */
+void fat_truncate_atime(struct msdos_sb_info *sbi, struct timespec64 *ts,
+			struct timespec64 *atime)

[...]

Quoted 2 lines
fs/fat/misc.c +2 -0
+void fat_truncate_crtime(struct msdos_sb_info *sbi, struct timespec64 *ts,
+			 struct timespec64 *crtime)

[...]

fs/fat/misc.c static inline struct timespec64 fat_timespec64_trunc_10ms(struct timespec64 ts) · line 320

Quoted 2 lines
fs/fat/misc.c +2 -0
+void fat_truncate_mtime(struct msdos_sb_info *sbi, struct timespec64 *ts,
+			struct timespec64 *mtime)

Small stuff and not strong opinion though, those are better to return timespec64, instead of taking pointer? Because we can

	mtime = ctime = fat_truncate_mtime(sbi, &ts);

Thanks.

Signature · 1 line
OGAWA Hirofumi <[email protected]>
#6 lore raw
Quoted 11 lines

fs/fat/misc.c static inline struct timespec64 fat_timespec64_trunc_10ms(struct timespec64 ts) · line 320

Quoted 6 lines
fs/fat/misc.c +6 -0
+void fat_truncate_atime(struct msdos_sb_info *sbi, struct timespec64 *ts,
+                     struct timespec64 *atime)
+void fat_truncate_crtime(struct msdos_sb_info *sbi, struct timespec64 *ts,
+                      struct timespec64 *crtime)
+void fat_truncate_mtime(struct msdos_sb_info *sbi, struct timespec64 *ts,
+                     struct timespec64 *mtime)

Small stuff and not strong opinion though, those are better to return timespec64, instead of taking pointer? Because we can

        mtime = ctime = fat_truncate_mtime(sbi, &ts);

It's ok. I can change them to this style.

Thanks.