Why does not the atime option of mount always update atime of files as expected?
I needed to detect read access to certain files so I needed the file system to update the atime
attribute of files the usual way.
Default mount options
In the default state the file system did not update atime
as expected though I did not use the noatime
option:
$ cat /etc/fstab | grep '/home '
# /home was on /dev/sda7 during installation
UUID=d7e67903-f24d-45a7-be90-6a134c9c1ae9 /home ext4 defaults 0 2
$ mount | grep '/home '
/dev/sda7 on /home type ext4 (rw)
$ cat /etc/mtab | grep '/home '
/dev/sda7 /home ext4 rw 0 0
The atime
on a file was updated only sometimes. Later I realized that the file system is mounted with the new relatime
option.
$ cat /proc/mounts | grep '/home '
/dev/sda7 /home ext4 rw,relatime,user_xattr,barrier=1,data=ordered 0 0
The obvious solution does not work
So it seems that the relatime
option is the default on Ubuntu 12.04. The solution seemed to be easy - explicitly state the non-default atime
option:
$ sudo mount -o remount,atime /home
I was suprised that the actual mount
options looked exactly the same like without the atime
option.
$ mount | grep '/home '
/dev/sda7 on /home type ext4 (rw)
$ cat /etc/mtab | grep '/home '
/dev/sda7 /home ext4 rw 0 0
$ cat /proc/mounts | grep '/home '
/dev/sda7 /home ext4 rw,relatime,user_xattr,barrier=1,data=ordered 0 0
In fact the system was behaving exactly the same as without the atime
option.
Questions
What is going on? Why cannot I simply use the atime
option? What can I do to get the normal atime
functionality?
In fact there are four five atime
-related options of mount
in Linux:
-
noatime
-atime
attribute (of both files and directories) is updated only during write access. -
nodiratime
-atime
of directories is updated only during write access, can be combined with relatime. -
relatime
/atime
-atime
seems to be now alias ofrelatime
. This option causes theatime
attribute to update only if the previousatime
is older thanmtime
orctime
, or the previousatime
is over 24 hours old. -
strictatime
- It was formerly calledatime
and as such was the default option. It causes theatime
attribute to update with every file access. (accessing file data, not just the metadata/attributes) -
lazytime
- Linux 4.10 added a new option: only update times (atime, mtime, ctime) on the in-memory version of the file inode. The on-disk data is updated lazily (seeman 8 mount
for details).
Since Linux kernel 2.6.30 the default option is relatime
. If you want to have the original atime
functionality you must use the strictatime
option. In the presented case it would be:
sudo mount -o remount,strictatime /home
Sources
stat()
-atime
problems solutions/etc/fstab
- common options to all file systems- Once upon a time