Skip to contentSkip to conversation

Relative lazy atime

RFC PATCH In review Valerie Henson 2 participants 2 messages Aug 3, 2006 VFS

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 Relative lazy atime

My friend Akkana followed my advice to use noatime on one of her machines, but discovered that mutt was unusable because it always thought that new messages had arrived since the last time it had checked a folder (mbox format). I thought this was a bummer, so I wrote a "relative lazy atime" patch which only updates the atime if the old atime was less than the ctime or mtime. This is not the same as the lazy atime patch of yore[1], which maintained a list of inodes with dirty atimes and wrote them out on unmount.

Patch below. Current version (plus test program) is at:

http://infohost.nmt.edu/~val/patches.html#lazyatime

Thanks to everyone who discussed this with me, especially the folks on #linuxfs on irc.oftc.net.

-VAL

[1] http://www.ussg.iu.edu/hypermail/linux/kernel/0005.0/0284.html

---

Relative lazy atime - only update atime if the old atime was older than the ctime or mtime. We are maintaining atime relative to mtime/ctime.

Not for inclusion (yet).

Written by Val Henson <[email protected]>

 fs/inode.c            |   16 +++++++++++++++-
 fs/namespace.c        |    5 ++++-
 include/linux/fs.h    |    1 +
 include/linux/mount.h |    1 +
 4 files changed, 21 insertions(+), 2 deletions(-)
fs/inode.c +15 -1
diff -Nrup a/fs/inode.c b/fs/inode.c
@@ -1200,10 +1200,24 @@ void touch_atime(struct vfsmount *mnt, s
 		return;
 
 	now = current_fs_time(inode->i_sb);
-	if (!timespec_equal(&inode->i_atime, &now)) {
+	if (timespec_equal(&inode->i_atime, &now))
+		return;
+	/*
+	 * With lazy atime, only update atime if the previous atime is
+	 * earlier than either the ctime or mtime.
+	 */
+	if (!mnt ||
+	    !(mnt->mnt_flags & MNT_LAZYATIME) ||
+	    (timespec_compare(&inode->i_atime, &inode->i_mtime) < 0) ||
+	    (timespec_compare(&inode->i_atime, &inode->i_ctime) < 0)) {
 		inode->i_atime = now;
 		mark_inode_dirty_sync(inode);
 	}
+	/*
+	 * We could update the in-memory atime here if we wanted, but
+	 * that makes it possible for atime to revert if we evict the
+	 * inode from memory.
+	 */
 }
 
 EXPORT_SYMBOL(touch_atime);
fs/namespace.c +4 -1
diff -Nrup a/fs/namespace.c b/fs/namespace.c
@@ -376,6 +376,7 @@ static int show_vfsmnt(struct seq_file *
 		{ MNT_NOEXEC, ",noexec" },
 		{ MNT_NOATIME, ",noatime" },
 		{ MNT_NODIRATIME, ",nodiratime" },
+		{ MNT_LAZYATIME, ",lazyatime" },
 		{ 0, NULL }
 	};
 	struct proc_fs_info *fs_infop;
@@ -1413,9 +1414,11 @@ long do_mount(char *dev_name, char *dir_
 		mnt_flags |= MNT_NOATIME;
 	if (flags & MS_NODIRATIME)
 		mnt_flags |= MNT_NODIRATIME;
+	if (flags & MS_LAZYATIME)
+		mnt_flags |= MNT_LAZYATIME;
 
 	flags &= ~(MS_NOSUID | MS_NOEXEC | MS_NODEV | MS_ACTIVE |
-		   MS_NOATIME | MS_NODIRATIME);
+		   MS_NOATIME | MS_NODIRATIME | MS_LAZYATIME);
 
 	/* ... and get the mountpoint */
 	retval = path_lookup(dir_name, LOOKUP_FOLLOW, &nd);
include/linux/fs.h +1 -0
diff -Nrup a/include/linux/fs.h b/include/linux/fs.h
@@ -119,6 +119,7 @@ extern int dir_notify_enable;
 #define MS_PRIVATE	(1<<18)	/* change to private */
 #define MS_SLAVE	(1<<19)	/* change to slave */
 #define MS_SHARED	(1<<20)	/* change to shared */
+#define MS_LAZYATIME	(1<<21)	/* Lazily update on-disk access times. */
 #define MS_ACTIVE	(1<<30)
 #define MS_NOUSER	(1<<31)
 
include/linux/mount.h +1 -0
diff -Nrup a/include/linux/mount.h b/include/linux/mount.h
@@ -27,6 +27,7 @@ struct namespace;
 #define MNT_NOEXEC	0x04
 #define MNT_NOATIME	0x08
 #define MNT_NODIRATIME	0x10
+#define MNT_LAZYATIME	0x20
 
 #define MNT_SHRINKABLE	0x100
 
#1 lore raw
Quoted 8 lines · Valerie Henson

My friend Akkana followed my advice to use noatime on one of her machines, but discovered that mutt was unusable because it always thought that new messages had arrived since the last time it had checked a folder (mbox format). I thought this was a bummer, so I wrote a "relative lazy atime" patch which only updates the atime if the old atime was less than the ctime or mtime. This is not the same as the lazy atime patch of yore[1], which maintained a list of inodes with dirty atimes and wrote them out on unmount.

This also happens to be useful for file systems such as Unionfs and (from what I hear) ocfs2 where atime updates can be costly at times.

Quoted 1 line

Patch below. Current version (plus test program) is at:

Looks fine IMHO.

Josef "Jeff" Sipek.

Signature · 2 lines
I'm somewhere between geek and normal.
		- Linus Torvalds