Commit b5cbe7c0 authored by Linus Torvalds's avatar Linus Torvalds
Browse files

Merge tag 'v6.6-rc3.vfs.ctime.revert' of git://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs

Pull finegrained timestamp reverts from Christian Brauner:
 "Earlier this week we sent a few minor fixes for the multi-grained
  timestamp work in [1]. While we were polishing those up after Linus
  realized that there might be a nicer way to fix them we received a
  regression report in [2] that fine grained timestamps break gnulib
  tests and thus possibly other tools.

  The kernel will elide fine-grain timestamp updates when no one is
  actively querying for them to avoid performance impacts. So a sequence
  like write(f1) stat(f2) write(f2) stat(f2) write(f1) stat(f1) may
  result in timestamp f1 to be older than the final f2 timestamp even
  though f1 was last written too but the second write didn't update the
  timestamp.

  Such plotholes can lead to subtle bugs when programs compare
  timestamps. For example, the nap() function in [2] will estimate that
  it needs to wait one ns on a fine-grain timestamp enabled filesytem
  between subsequent calls to observe a timestamp change. But in general
  we don't update timestamps with more than one jiffie if we think that
  no one is actively querying for fine-grain timestamps to avoid
  performance impacts.

  While discussing various fixes the decision was to go back to the
  drawing board and ultimately to explore a solution that involves only
  exposing such fine-grained timestamps to nfs internally and never to
  userspace.

  As there are multiple solutions discussed the honest thing to do here
  is not to fix this up or disable it but to cleanly revert. The general
  infrastructure will probably come back but there is no reason to keep
  this code in mainline.

  The general changes to timestamp handling are valid and a good cleanup
  that will stay. The revert is fully bisectable"

Link: https://lore.kernel.org/all/20230918-hirte-neuzugang-4c2324e7bae3@brauner [1]
Link: https://lore.kernel.org/all/bf0524debb976627693e12ad23690094e4514303.camel@linuxfromscratch.org [2]

* tag 'v6.6-rc3.vfs.ctime.revert' of git://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs:
  Revert "fs: add infrastructure for multigrain timestamps"
  Revert "btrfs: convert to multigrain timestamps"
  Revert "ext4: switch to multigrain timestamps"
  Revert "xfs: switch to multigrain timestamps"
  Revert "tmpfs: add support for multigrain timestamps"
parents 7bdfc1af 647aa768
Loading
Loading
Loading
Loading
+20 −4
Original line number Diff line number Diff line
@@ -1106,6 +1106,25 @@ void btrfs_check_nocow_unlock(struct btrfs_inode *inode)
	btrfs_drew_write_unlock(&inode->root->snapshot_lock);
}

static void update_time_for_write(struct inode *inode)
{
	struct timespec64 now, ctime;

	if (IS_NOCMTIME(inode))
		return;

	now = current_time(inode);
	if (!timespec64_equal(&inode->i_mtime, &now))
		inode->i_mtime = now;

	ctime = inode_get_ctime(inode);
	if (!timespec64_equal(&ctime, &now))
		inode_set_ctime_to_ts(inode, now);

	if (IS_I_VERSION(inode))
		inode_inc_iversion(inode);
}

static int btrfs_write_check(struct kiocb *iocb, struct iov_iter *from,
			     size_t count)
{
@@ -1137,10 +1156,7 @@ static int btrfs_write_check(struct kiocb *iocb, struct iov_iter *from,
	 * need to start yet another transaction to update the inode as we will
	 * update the inode when we finish writing whatever data we write.
	 */
	if (!IS_NOCMTIME(inode)) {
		inode->i_mtime = inode_set_ctime_current(inode);
		inode_inc_iversion(inode);
	}
	update_time_for_write(inode);

	start_pos = round_down(pos, fs_info->sectorsize);
	oldsize = i_size_read(inode);
+2 −3
Original line number Diff line number Diff line
@@ -2150,7 +2150,7 @@ static struct file_system_type btrfs_fs_type = {
	.name		= "btrfs",
	.mount		= btrfs_mount,
	.kill_sb	= btrfs_kill_super,
	.fs_flags	= FS_REQUIRES_DEV | FS_BINARY_MOUNTDATA | FS_MGTIME,
	.fs_flags	= FS_REQUIRES_DEV | FS_BINARY_MOUNTDATA,
};

static struct file_system_type btrfs_root_fs_type = {
@@ -2158,8 +2158,7 @@ static struct file_system_type btrfs_root_fs_type = {
	.name		= "btrfs",
	.mount		= btrfs_mount_root,
	.kill_sb	= btrfs_kill_super,
	.fs_flags	= FS_REQUIRES_DEV | FS_BINARY_MOUNTDATA |
			  FS_ALLOW_IDMAP | FS_MGTIME,
	.fs_flags	= FS_REQUIRES_DEV | FS_BINARY_MOUNTDATA | FS_ALLOW_IDMAP,
};

MODULE_ALIAS_FS("btrfs");
+1 −1
Original line number Diff line number Diff line
@@ -7314,7 +7314,7 @@ static struct file_system_type ext4_fs_type = {
	.init_fs_context	= ext4_init_fs_context,
	.parameters		= ext4_param_specs,
	.kill_sb		= ext4_kill_sb,
	.fs_flags		= FS_REQUIRES_DEV | FS_ALLOW_IDMAP | FS_MGTIME,
	.fs_flags		= FS_REQUIRES_DEV | FS_ALLOW_IDMAP,
};
MODULE_ALIAS_FS("ext4");

+3 −79
Original line number Diff line number Diff line
@@ -2102,52 +2102,10 @@ int file_remove_privs(struct file *file)
}
EXPORT_SYMBOL(file_remove_privs);

/**
 * current_mgtime - Return FS time (possibly fine-grained)
 * @inode: inode.
 *
 * Return the current time truncated to the time granularity supported by
 * the fs, as suitable for a ctime/mtime change. If the ctime is flagged
 * as having been QUERIED, get a fine-grained timestamp.
 */
struct timespec64 current_mgtime(struct inode *inode)
{
	struct timespec64 now, ctime;
	atomic_long_t *pnsec = (atomic_long_t *)&inode->__i_ctime.tv_nsec;
	long nsec = atomic_long_read(pnsec);

	if (nsec & I_CTIME_QUERIED) {
		ktime_get_real_ts64(&now);
		return timestamp_truncate(now, inode);
	}

	ktime_get_coarse_real_ts64(&now);
	now = timestamp_truncate(now, inode);

	/*
	 * If we've recently fetched a fine-grained timestamp
	 * then the coarse-grained one may still be earlier than the
	 * existing ctime. Just keep the existing value if so.
	 */
	ctime = inode_get_ctime(inode);
	if (timespec64_compare(&ctime, &now) > 0)
		now = ctime;

	return now;
}
EXPORT_SYMBOL(current_mgtime);

static struct timespec64 current_ctime(struct inode *inode)
{
	if (is_mgtime(inode))
		return current_mgtime(inode);
	return current_time(inode);
}

static int inode_needs_update_time(struct inode *inode)
{
	int sync_it = 0;
	struct timespec64 now = current_ctime(inode);
	struct timespec64 now = current_time(inode);
	struct timespec64 ctime;

	/* First try to exhaust all avenues to not sync */
@@ -2578,43 +2536,9 @@ EXPORT_SYMBOL(current_time);
 */
struct timespec64 inode_set_ctime_current(struct inode *inode)
{
	struct timespec64 now;
	struct timespec64 ctime;

	ctime.tv_nsec = READ_ONCE(inode->__i_ctime.tv_nsec);
	if (!(ctime.tv_nsec & I_CTIME_QUERIED)) {
		now = current_time(inode);
	struct timespec64 now = current_time(inode);

		/* Just copy it into place if it's not multigrain */
		if (!is_mgtime(inode)) {
			inode_set_ctime_to_ts(inode, now);
			return now;
		}

		/*
		 * If we've recently updated with a fine-grained timestamp,
		 * then the coarse-grained one may still be earlier than the
		 * existing ctime. Just keep the existing value if so.
		 */
		ctime.tv_sec = inode->__i_ctime.tv_sec;
		if (timespec64_compare(&ctime, &now) > 0)
			return ctime;

		/*
		 * Ctime updates are usually protected by the inode_lock, but
		 * we can still race with someone setting the QUERIED flag.
		 * Try to swap the new nsec value into place. If it's changed
		 * in the interim, then just go with a fine-grained timestamp.
		 */
		if (cmpxchg(&inode->__i_ctime.tv_nsec, ctime.tv_nsec,
			    now.tv_nsec) != ctime.tv_nsec)
			goto fine_grained;
		inode->__i_ctime.tv_sec = now.tv_sec;
		return now;
	}
fine_grained:
	ktime_get_real_ts64(&now);
	inode_set_ctime_to_ts(inode, timestamp_truncate(now, inode));
	inode_set_ctime(inode, now.tv_sec, now.tv_nsec);
	return now;
}
EXPORT_SYMBOL(inode_set_ctime_current);
+2 −39
Original line number Diff line number Diff line
@@ -26,37 +26,6 @@
#include "internal.h"
#include "mount.h"

/**
 * fill_mg_cmtime - Fill in the mtime and ctime and flag ctime as QUERIED
 * @stat: where to store the resulting values
 * @request_mask: STATX_* values requested
 * @inode: inode from which to grab the c/mtime
 *
 * Given @inode, grab the ctime and mtime out if it and store the result
 * in @stat. When fetching the value, flag it as queried so the next write
 * will use a fine-grained timestamp.
 */
void fill_mg_cmtime(struct kstat *stat, u32 request_mask, struct inode *inode)
{
	atomic_long_t *pnsec = (atomic_long_t *)&inode->__i_ctime.tv_nsec;

	/* If neither time was requested, then don't report them */
	if (!(request_mask & (STATX_CTIME|STATX_MTIME))) {
		stat->result_mask &= ~(STATX_CTIME|STATX_MTIME);
		return;
	}

	stat->mtime = inode->i_mtime;
	stat->ctime.tv_sec = inode->__i_ctime.tv_sec;
	/*
	 * Atomically set the QUERIED flag and fetch the new value with
	 * the flag masked off.
	 */
	stat->ctime.tv_nsec = atomic_long_fetch_or(I_CTIME_QUERIED, pnsec) &
					~I_CTIME_QUERIED;
}
EXPORT_SYMBOL(fill_mg_cmtime);

/**
 * generic_fillattr - Fill in the basic attributes from the inode struct
 * @idmap:		idmap of the mount the inode was found from
@@ -89,14 +58,8 @@ void generic_fillattr(struct mnt_idmap *idmap, u32 request_mask,
	stat->rdev = inode->i_rdev;
	stat->size = i_size_read(inode);
	stat->atime = inode->i_atime;

	if (is_mgtime(inode)) {
		fill_mg_cmtime(stat, request_mask, inode);
	} else {
	stat->mtime = inode->i_mtime;
	stat->ctime = inode_get_ctime(inode);
	}

	stat->blksize = i_blocksize(inode);
	stat->blocks = inode->i_blocks;

Loading