Skip to contentSkip to conversation

fat: split fat_truncate_time() into separate functions

PATCH Superseded Chung-Chiang Cheng 3 participants 8 messages Apr 15, 2022 – Apr 22, 2022 VFAT/FAT/msdos filesystem

v3 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 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 v3 2/3 fat: make ctime and mtime identical explicitly

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

---
 fs/fat/dir.c         |  2 +-
 fs/fat/file.c        |  4 ++++
 fs/fat/inode.c       | 11 ++++-------
 fs/fat/misc.c        | 11 ++++++++---
 fs/fat/namei_msdos.c |  6 +++---
 fs/fat/namei_vfat.c  |  6 +++---
 6 files changed, 23 insertions(+), 17 deletions(-)
fs/fat/dir.c +1 -1
index 249825017da7..0ae0dfe278fb 100644
@@ -1068,7 +1068,7 @@ int fat_remove_entries(struct inode *dir, struct fat_slot_info *sinfo)
 		}
 	}
 
-	fat_truncate_time(dir, NULL, S_ATIME|S_MTIME);
+	fat_truncate_time(dir, NULL, S_ATIME|S_CTIME|S_MTIME);
 	if (IS_DIRSYNC(dir))
 		(void)fat_sync_inode(dir);
 	else
fs/fat/file.c +4 -0
index a5a309fcc7fa..178c1dde3488 100644
@@ -544,6 +544,10 @@ int fat_setattr(struct user_namespace *mnt_userns, struct dentry *dentry,
 	/*
 	 * setattr_copy can't truncate these appropriately, so we'll
 	 * copy them ourselves
+	 *
+	 * fat_truncate_time() keeps ctime and mtime the same. if both
+	 * ctime and mtime are need to update here, mtime will overwrite
+	 * ctime
 	 */
 	if (attr->ia_valid & ATTR_ATIME)
 		fat_truncate_time(inode, &attr->ia_atime, S_ATIME);
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 +8 -3
index c87df64f8b2b..71e6dadf12a2 100644
@@ -341,10 +341,15 @@ 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.
+	 */
+	if (flags & (S_CTIME|S_MTIME)) {
 		fat_truncate_mtime(sbi, now, &inode->i_mtime);
+		inode->i_ctime = inode->i_mtime;
+	}
 
 	return 0;
 }
fs/fat/namei_msdos.c +3 -3
index efba301d68ae..b2760a716707 100644
@@ -328,7 +328,7 @@ static int msdos_rmdir(struct inode *dir, struct dentry *dentry)
 	drop_nlink(dir);
 
 	clear_nlink(inode);
-	fat_truncate_time(inode, NULL, S_CTIME);
+	fat_truncate_time(inode, NULL, S_CTIME|S_MTIME);
 	fat_detach(inode);
 out:
 	mutex_unlock(&MSDOS_SB(sb)->s_lock);
@@ -415,7 +415,7 @@ static int msdos_unlink(struct inode *dir, struct dentry *dentry)
 	if (err)
 		goto out;
 	clear_nlink(inode);
-	fat_truncate_time(inode, NULL, S_CTIME);
+	fat_truncate_time(inode, NULL, S_CTIME|S_MTIME);
 	fat_detach(inode);
 out:
 	mutex_unlock(&MSDOS_SB(sb)->s_lock);
@@ -550,7 +550,7 @@ static int do_msdos_rename(struct inode *old_dir, unsigned char *old_name,
 		drop_nlink(new_inode);
 		if (is_dir)
 			drop_nlink(new_inode);
-		fat_truncate_time(new_inode, &ts, S_CTIME);
+		fat_truncate_time(new_inode, &ts, S_CTIME|S_MTIME);
 	}
 out:
 	brelse(sinfo.bh);
fs/fat/namei_vfat.c +3 -3
index 5369d82e0bfb..b8deb859b2b5 100644
@@ -811,7 +811,7 @@ static int vfat_rmdir(struct inode *dir, struct dentry *dentry)
 	drop_nlink(dir);
 
 	clear_nlink(inode);
-	fat_truncate_time(inode, NULL, S_ATIME|S_MTIME);
+	fat_truncate_time(inode, NULL, S_ATIME|S_CTIME|S_MTIME);
 	fat_detach(inode);
 	vfat_d_version_set(dentry, inode_query_iversion(dir));
 out:
@@ -837,7 +837,7 @@ static int vfat_unlink(struct inode *dir, struct dentry *dentry)
 	if (err)
 		goto out;
 	clear_nlink(inode);
-	fat_truncate_time(inode, NULL, S_ATIME|S_MTIME);
+	fat_truncate_time(inode, NULL, S_ATIME|S_CTIME|S_MTIME);
 	fat_detach(inode);
 	vfat_d_version_set(dentry, inode_query_iversion(dir));
 out:
@@ -981,7 +981,7 @@ static int vfat_rename(struct user_namespace *mnt_userns, struct inode *old_dir,
 		drop_nlink(new_inode);
 		if (is_dir)
 			drop_nlink(new_inode);
-		fat_truncate_time(new_inode, &ts, S_CTIME);
+		fat_truncate_time(new_inode, &ts, S_CTIME|S_MTIME);
 	}
 out:
 	brelse(sinfo.bh);
Signature · 1 line
2.34.1
maintainer
#2 lore raw

Re: [PATCH v3 2/3] fat: make ctime and mtime identical explicitly

fs/fat/dir.c int fat_remove_entries(struct inode *dir, struct fat_slot_info *sinfo) · line 1071

Quoted 2 lines · Chung-Chiang Cheng
fs/fat/dir.c +1 -1
-	fat_truncate_time(dir, NULL, S_ATIME|S_MTIME);
+	fat_truncate_time(dir, NULL, S_ATIME|S_CTIME|S_MTIME);

fat_truncate_time() updates i_ctime too. So S_CTIME should not be necessary here. And I think this is better to use only S_MTIME to tell this is the point of mtime update.

(And, in fat_truncate_time(), I think S_CTIME is not required, because we ignore ctime change, isn't it?)

Or you are going to update mtime on rename, etc too?

Quoted 8 lines
fs/fat/misc.c +7 -0
+	/*
+	 * ctime and mtime share the same on-disk field, and should be
+	 * identical in memory.
+	 */
+	if (flags & (S_CTIME|S_MTIME)) {
 		fat_truncate_mtime(sbi, now, &inode->i_mtime);
+		inode->i_ctime = inode->i_mtime;
+	}

[...]

fs/fat/namei_msdos.c static int msdos_rmdir(struct inode *dir, struct dentry *dentry) · line 331

Quoted 3 lines
fs/fat/namei_msdos.c +1 -1
 	clear_nlink(inode);
-	fat_truncate_time(inode, NULL, S_CTIME);
+	fat_truncate_time(inode, NULL, S_CTIME|S_MTIME);

This is the point to update ctime. You want to affect ctime change to mtime? As I said in previous post, I think we are better to ignore ctime change, because it may become yet another incompatible behavior.

fs/fat/namei_msdos.c static int msdos_unlink(struct inode *dir, struct dentry *dentry) · line 418

Quoted 9 lines
fs/fat/namei_msdos.c +1 -1
 	fat_detach(inode);
 out:
 	mutex_unlock(&MSDOS_SB(sb)->s_lock);
@@ -415,7 +415,7 @@ static int msdos_unlink(struct inode *dir, struct dentry *dentry)
 	if (err)
 		goto out;
 	clear_nlink(inode);
-	fat_truncate_time(inode, NULL, S_CTIME);
+	fat_truncate_time(inode, NULL, S_CTIME|S_MTIME);

ditto

fs/fat/namei_msdos.c static int do_msdos_rename(struct inode *old_dir, unsigned char *old_name, · line 553

Quoted 9 lines
fs/fat/namei_msdos.c +1 -1
 	fat_detach(inode);
 out:
 	mutex_unlock(&MSDOS_SB(sb)->s_lock);
@@ -550,7 +550,7 @@ static int do_msdos_rename(struct inode *old_dir, unsigned char *old_name,
 		drop_nlink(new_inode);
 		if (is_dir)
 			drop_nlink(new_inode);
-		fat_truncate_time(new_inode, &ts, S_CTIME);
+		fat_truncate_time(new_inode, &ts, S_CTIME|S_MTIME);

ditto

Quoted 2 lines
(quoted diff) +0 -0
 	}
 out:

[...]

fs/fat/namei_msdos.c static int vfat_rename(struct user_namespace *mnt_userns, struct inode *old_dir, · line 984

Quoted 6 lines
fs/fat/namei_msdos.c +1 -1
@@ -981,7 +981,7 @@ static int vfat_rename(struct user_namespace *mnt_userns, struct inode *old_dir,
 		drop_nlink(new_inode);
 		if (is_dir)
 			drop_nlink(new_inode);
-		fat_truncate_time(new_inode, &ts, S_CTIME);
+		fat_truncate_time(new_inode, &ts, S_CTIME|S_MTIME);

ditto

Thanks.

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

Re: [PATCH v3 2/3] fat: make ctime and mtime identical explicitly

Quoted 13 lines · OGAWA Hirofumi

fs/fat/dir.c int fat_remove_entries(struct inode *dir, struct fat_slot_info *sinfo) · line 1071

Quoted 2 lines · Chung-Chiang Cheng
fs/fat/dir.c +1 -1
-     fat_truncate_time(dir, NULL, S_ATIME|S_MTIME);
+     fat_truncate_time(dir, NULL, S_ATIME|S_CTIME|S_MTIME);

fat_truncate_time() updates i_ctime too. So S_CTIME should not be necessary here. And I think this is better to use only S_MTIME to tell this is the point of mtime update.

(And, in fat_truncate_time(), I think S_CTIME is not required, because we ignore ctime change, isn't it?)

Quoted 8 lines

Review

Quoted 3 lines
      clear_nlink(inode);
(quoted diff) +1 -1
-     fat_truncate_time(inode, NULL, S_CTIME);
+     fat_truncate_time(inode, NULL, S_CTIME|S_MTIME);

This is the point to update ctime. You want to affect ctime change to mtime? As I said in previous post, I think we are better to ignore ctime change, because it may become yet another incompatible behavior.

Thanks for the feedback. I will change the behavior to ignore ctime updates in the next version of the patch.

#4 lore raw

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

In reply to Chung-Chiang Cheng

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

---
 fs/fat/fat.h   |  1 +
 fs/fat/file.c  | 14 +++++++++++---
 fs/fat/inode.c | 12 ++++++++++--
 3 files changed, 22 insertions(+), 5 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 178c1dde3488..7a4f617d4928 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 +10 -2
index f2ac55cd4ea4..23fac3181fa7 100644
@@ -568,10 +568,14 @@ 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);
+		fat_truncate_crtime(sbi, &inode->i_mtime, &MSDOS_I(inode)->i_crtime);
+	}
 
 	return 0;
 }
@@ -756,6 +760,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 +893,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);
 	}
Signature · 1 line
2.34.1
maintainer
#5 lore raw

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

Quoted 4 lines · Chung-Chiang Cheng
fs/fat/inode.c +0 -0
@@ -568,10 +568,14 @@ 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;

fs/fat/inode.c int fat_fill_inode(struct inode *inode, struct msdos_dir_entry *de) · line 578

Quoted 10 lines
fs/fat/inode.c +6 -2
-	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);
+		fat_truncate_crtime(sbi, &inode->i_mtime, &MSDOS_I(inode)->i_crtime);
+	}

Probably, nothing worth to update i_crtime here, right?

Thanks.

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

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

Quoted 7 lines

fs/fat/inode.c int fat_fill_inode(struct inode *inode, struct msdos_dir_entry *de) · line 578

Quoted 4 lines
fs/fat/inode.c +3 -0
+     } else {
              fat_truncate_atime(sbi, &inode->i_mtime, &inode->i_atime);
+             fat_truncate_crtime(sbi, &inode->i_mtime, &MSDOS_I(inode)->i_crtime);
+     }

Probably, nothing worth to update i_crtime here, right?

That's right. I also miss truncating the create-time for newly created files in vfat. I will add it back in the next patch. It will be the only location that calls fat_truncate_crtime().

@@ -781,11 +782,12 @@ 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(...)

I also have a question. If msdos doesn't support create-time, why does the original fat_truncate_time() truncate it to a 2-sec granularity?

if (flags & S_CTIME) {
        if (sbi->options.isvfat)
                inode->i_ctime = fat_timespec64_trunc_10ms(*now);
        else
                inode->i_ctime = fat_timespec64_trunc_2secs(*now);
}

This logic was introduced in 6bb885ecd746 (fat: add functions to update and truncate timestamps appropriately"), and seems not for change-time. Should I remove it?

Thanks.

maintainer
#7 lore raw

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

Quoted 11 lines · Chung-Chiang Cheng

I also have a question. If msdos doesn't support create-time, why does the original fat_truncate_time() truncate it to a 2-sec granularity?

if (flags & S_CTIME) {
        if (sbi->options.isvfat)
                inode->i_ctime = fat_timespec64_trunc_10ms(*now);
        else
                inode->i_ctime = fat_timespec64_trunc_2secs(*now);
}
Quoted 3 lines

This logic was introduced in 6bb885ecd746 (fat: add functions to update and truncate timestamps appropriately"), and seems not for change-time. Should I remove it?

Hm, not sure.

However, ctime/atime is not stored into backing storage in __fat_write_inode() and loaded if !isvfat (if I'm not missing something). So I think we don't need it for creation time.

Thanks.

Signature · 1 line
OGAWA Hirofumi <[email protected]>