Re: [RFC PATCH 1/3] fs: add infrastructure for opportunistic high-res ctime/mtime updates

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Fri, 2023-04-21 at 12:13 +0200, Jan Kara wrote:
> On Tue 11-04-23 10:27:06, Jeff Layton wrote:
> > The VFS always uses coarse-grained timestamp updates for filling out the
> > ctime and mtime after a change. This has the benefit of allowing
> > filesystems to optimize away metadata updates.
> > 
> > Unfortunately, this has always been an issue when we're exporting via
> > NFSv3, which relies on timestamps to validate caches. Even with NFSv4, a
> > lot of exported filesystems don't properly support a change attribute
> > and are subject to the same problem of timestamp granularity. Other
> > applications have similar issues (e.g backup applications).
> > 
> > Switching to always using high resolution timestamps would improve the
> > situation for NFS, but that becomes rather expensive, as we'd have to
> > log a lot more metadata updates.
> > 
> > This patch grabs a new i_state bit to use as a flag that filesystems can
> > set in their getattr routine to indicate that the mtime or ctime was
> > queried since it was last updated.
> > 
> > It then adds a new current_cmtime function that acts like the
> > current_time helper, but will conditionally grab high-res timestamps
> > when the i_state flag is set in the inode.
> > 
> > This allows NFS and other applications to reap the benefits of high-res
> > ctime and mtime timestamps, but at a substantially lower cost than
> > fetching them every time.
> > 
> > Cc: Dave Chinner <david@xxxxxxxxxxxxx>
> > Signed-off-by: Jeff Layton <jlayton@xxxxxxxxxx>
> > ---
> >  fs/inode.c         | 40 ++++++++++++++++++++++++++++++++++++++--
> >  fs/stat.c          | 10 ++++++++++
> >  include/linux/fs.h |  5 ++++-
> >  3 files changed, 52 insertions(+), 3 deletions(-)
> > 
> > diff --git a/fs/inode.c b/fs/inode.c
> > index 4558dc2f1355..3630f67fd042 100644
> > --- a/fs/inode.c
> > +++ b/fs/inode.c
> > @@ -2062,6 +2062,42 @@ static int __file_update_time(struct file *file, struct timespec64 *now,
> >  	return ret;
> >  }
> >  
> > +/**
> > + * current_cmtime - Return FS time (possibly high-res)
> > + * @inode: inode.
> > + *
> > + * Return the current time truncated to the time granularity supported by
> > + * the fs, as suitable for a ctime or mtime change. If something recently
> > + * fetched the ctime or mtime out of the inode via getattr, then get a
> > + * high-resolution timestamp.
> > + *
> > + * Note that inode and inode->sb cannot be NULL.
> > + * Otherwise, the function warns and returns coarse time without truncation.
> > + */
> > +struct timespec64 current_cmtime(struct inode *inode)
> > +{
> > +	struct timespec64 now;
> > +
> > +	if (unlikely(!inode->i_sb)) {
> 
> I don't think we can have inodes without a superblock. Did you ever hit
> this?
> 

No, I copied this from current_time. I've already removed this in my
working branch. We can probably remove it from current_time too.

> > +		WARN(1, "%s() called with uninitialized super_block in the inode", __func__);
> > +		ktime_get_coarse_real_ts64(&now);
> > +		return now;
> > +	}
> > +
> > +	/* Do a lockless check for the flag before taking the spinlock */
> > +	if (READ_ONCE(inode->i_state) & I_CMTIME_QUERIED) {
> > +		ktime_get_real_ts64(&now);
> > +		spin_lock(&inode->i_lock);
> > +		inode->i_state &= ~I_CMTIME_QUERIED;
> 
> Isn't this a bit fragile? If someone does:
> 
> 	inode->i_mtime = current_cmtime(inode);
> 	inode->i_ctime = current_cmtime(inode);
> 
> the ctime update will be coarse although it should be fine-grained.
> 

It is a bit. We'll need for users to do something like:

    inode->i_mtime = inode->i_ctime = current_ctime(inode);

Fortunately, most do this already.

> > +		spin_unlock(&inode->i_lock);
> > +	} else {
> > +		ktime_get_coarse_real_ts64(&now);
> > +	}
> > +
> > +	return timestamp_truncate(now, inode);
> 
> I'm a bit confused here. Isn't the point of this series also to give NFS
> finer grained granularity time stamps than what the filesystem is possibly
> able to store on disk?
> 

No. We actually don't want to hand out timestamps more granular than the
underlying filesystem can support, as we'd end up having to invalidate
caches for all of those inodes once the server rebooted and the
unrecordable bits get zeroed out.

The main idea here is to just ensure that we use fine-grained timestamps
when someone has queried the mtime or ctime since the last time it was
updated.

> Hmm, checking XFS it sets 1 ns granularity (as well as tmpfs) so for these
> using the coarser timers indeed gives a performance benefit. And probably
> you've decided not implement the "better NFS support with coarse grained
> timestamps" yet.
> 

Yep. The coarse grained timestamps are a _good_ thing for most
filesystems as they allow you to skip a lot of metadata updates. My hope
is that this will end up being like the i_version changes such that the
extra fine-grained updates should be relatively rare and should
(hopefully!) not cause noticeable performance blips. We'll see!

> > +}
> > +EXPORT_SYMBOL(current_cmtime);
> > +
> >  /**
> >   * file_update_time - update mtime and ctime time
> >   * @file: file accessed
> > @@ -2080,7 +2116,7 @@ int file_update_time(struct file *file)
> >  {
> >  	int ret;
> >  	struct inode *inode = file_inode(file);
> > -	struct timespec64 now = current_time(inode);
> > +	struct timespec64 now = current_cmtime(inode);
> >  
> >  	ret = inode_needs_update_time(inode, &now);
> >  	if (ret <= 0)
> > @@ -2109,7 +2145,7 @@ static int file_modified_flags(struct file *file, int flags)
> >  {
> >  	int ret;
> >  	struct inode *inode = file_inode(file);
> > -	struct timespec64 now = current_time(inode);
> > +	struct timespec64 now = current_cmtime(inode);
> >  
> >  	/*
> >  	 * Clear the security bits if the process is not being run by root.
> > diff --git a/fs/stat.c b/fs/stat.c
> > index 7c238da22ef0..d8b80a2e36b7 100644
> > --- a/fs/stat.c
> > +++ b/fs/stat.c
> > @@ -64,6 +64,16 @@ void generic_fillattr(struct mnt_idmap *idmap, struct inode *inode,
> >  }
> >  EXPORT_SYMBOL(generic_fillattr);
> >  
> > +void fill_cmtime_and_mark(struct inode *inode, struct kstat *stat)
> > +{
> > +	spin_lock(&inode->i_lock);
> > +	inode->i_state |= I_CMTIME_QUERIED;
> > +	stat->ctime = inode->i_ctime;
> > +	stat->mtime = inode->i_mtime;
> > +	spin_unlock(&inode->i_lock);
> > +}
> > +EXPORT_SYMBOL(fill_cmtime_and_mark);
> 
> The name could be better here :). Maybe stat_fill_cmtime_and_mark()?
> 
> 								

I have a quite different set that I've been working on that I'll
(hopefully!) post soon. That one uses the least significant bit of the
tv_nsec field as the QUERIED flag instead of the spinlock.

Still cleaning up the set and need to test it some more though, so it's
not quite ready to post. Stay tuned!

Thanks for the review! 
-- 
Jeff Layton <jlayton@xxxxxxxxxx>




[Index of Archives]     [XFS Filesystem Development (older mail)]     [Linux Filesystem Development]     [Linux Audio Users]     [Yosemite Trails]     [Linux Kernel]     [Linux RAID]     [Linux SCSI]


  Powered by Linux