Re: [PATCH 08/11] xfs: widen ondisk timestamps to deal with y2038 problem

Dave Chinner
2020-08-18T23:35:45Z

On Mon, Aug 17, 2020 at 03:57:39PM -0700, Darrick J. Wong wrote:
> From: Darrick J. Wong <darrick.wong@oracle.com>
>
> Redesign the ondisk timestamps to be a simple unsigned 64-bit counter of
> nanoseconds since 14 Dec 1901 (i.e. the minimum time in the 32-bit unix
> time epoch). This enables us to handle dates up to 2486, which solves
> the y2038 problem.
>
> Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
> ---
.....
> +/* Convert an ondisk timestamp into the 64-bit safe incore format. */
> void
> xfs_inode_from_disk_timestamp(
> + struct xfs_dinode *dip,
> struct timespec64 *tv,
> const union xfs_timestamp *ts)
> {
> + if (dip->di_version >= 3 &&
> + (dip->di_flags2 & cpu_to_be64(XFS_DIFLAG2_BIGTIME))) {
> + uint64_t t = be64_to_cpu(ts->t_bigtime);
> + uint64_t s;
> + uint32_t n;
> +
> + s = div_u64_rem(t, NSEC_PER_SEC, &n);
> + tv->tv_sec = s - XFS_INO_BIGTIME_EPOCH;
> + tv->tv_nsec = n;
> + return;
> + }
> +
> tv->tv_sec = (int)be32_to_cpu(ts->t_sec);
> tv->tv_nsec = (int)be32_to_cpu(ts->t_nsec);
> }

Can't say I'm sold on this union. It seems cleaner to me to just
make the timestamp an opaque 64 bit field on disk and convert it to
the in-memory representation directly in the to/from disk
operations. e.g.:

void
xfs_inode_from_disk_timestamp(
struct xfs_dinode *dip,
struct timespec64 *tv,
__be64 ts)
{

uint64_t t = be64_to_cpu(ts);
uint64_t s;
uint32_t n;

if (xfs_dinode_is_bigtime(dip)) {
s = div_u64_rem(t, NSEC_PER_SEC, &n) - XFS_INO_BIGTIME_EPOCH;
} else {
s = (int)(t >> 32);
n = (int)(t & 0xffffffff);
}
tv->tv_sec = s;
tv->tv_nsec = n;
}

> @@ -220,9 +234,9 @@ xfs_inode_from_disk(
> * a time before epoch is converted to a time long after epoch
> * on 64 bit systems.
> */
> - xfs_inode_from_disk_timestamp(&inode->i_atime, &from->di_atime);
> - xfs_inode_from_disk_timestamp(&inode->i_mtime, &from->di_mtime);
> - xfs_inode_from_disk_timestamp(&inode->i_ctime, &from->di_ctime);
> + xfs_inode_from_disk_timestamp(from, &inode->i_atime, &from->di_atime);
> + xfs_inode_from_disk_timestamp(from, &inode->i_mtime, &from->di_mtime);
> + xfs_inode_from_disk_timestamp(from, &inode->i_ctime, &from->di_ctime);
>
> to->di_size = be64_to_cpu(from->di_size);
> to->di_nblocks = be64_to_cpu(from->di_nblocks);
> @@ -235,9 +249,17 @@ xfs_inode_from_disk(
> if (xfs_sb_version_has_v3inode(&ip->i_mount->m_sb)) {
> inode_set_iversion_queried(inode,
> be64_to_cpu(from->di_changecount));
> - xfs_inode_from_disk_timestamp(&to->di_crtime, &from->di_crtime);
> + xfs_inode_from_disk_timestamp(from, &to->di_crtime,
> + &from->di_crtime);
> to->di_flags2 = be64_to_cpu(from->di_flags2);
> to->di_cowextsize = be32_to_cpu(from->di_cowextsize);
> + /*
> + * Set the bigtime flag incore so that we automatically convert
> + * this inode's ondisk timestamps to bigtime format the next
> + * time we write the inode core to disk.
> + */
> + if (xfs_sb_version_hasbigtime(&ip->i_mount->m_sb))
> + to->di_flags2 |= XFS_DIFLAG2_BIGTIME;
> }

We do not want on-disk flags to be changed outside transactions like
this. Indeed, this has implications for O_DSYNC operation, in that
we do not trigger inode sync operations if the inode is only
timestamp dirty. If we've changed this flag, then the inode is more
than "timestamp dirty" and O_DSYNC will need to flush the entire
inode.... :/

IOWs, I think we should only change this flag in a timestamp
transaction where the timestamps are actually being logged and hence
we can set inode dirty state appropriately so that everything will
get logged, changed and written back correctly....

Cheers,

Dave.
--
Dave Chinner
david@fromorbit.com