先简单介绍下atime/mtime/ctime
atime: access time,表明读文件内容的时间。access time
mtime:修改文件内存的时间。modify time
ctime:修改文件内容的时间,文件属性的时间。change time
也就是说修改mtime一定修改ctime,但是修改ctime未必修改mtime,比如chmod/chown只会修改ctime,不会修改mtime。
何时更新atime?
一般认为读了文件就需要更新atime,但是实际不是这样的。更新atime的内核调用函数
file_accessed()--->touch_atime()--->update_atime.
在touch_atime中有很多的判断信息,判断文件是否需要更新atime,比如挂载时noatime等。在这里重点是关注:relatime_need_update
/*
* With relative atime, only update atime if the previous atime is
* earlier than either the ctime or mtime or if at least a day has
* passed since the last atime update.
*/
static int relatime_need_update(struct vfsmount *mnt, struct inode *inode,
struct timespec now)
{
if (!(mnt->mnt_flags & MNT_RELATIME))
return 1;
/*
* Is mtime younger than atime? If yes, update atime:
*/
if (timespec_compare(&inode->i_mtime, &inode->i_atime) >= 0)
return 1;
/*
* Is ctime younger than atime? If yes, update atime:
*/
if (timespec_compare(&inode->i_ctime, &inode->i_atime) >= 0)
return 1;
/*
* Is the previous atime value older than a day? If yes,
* update atime:
*/
if ((long)(now.tv_sec - inode->i_atime.tv_sec) >= 24*60*60)
return 1;
/*
* Good, we can skip the atime update:
*/
return 0;
}
是否要更新atime要满足:
1:mtime 或者ctime的更新要新于atime,即atime老一点
2:两次atime的更新时间需要大于1天
对于第一个条件,可以认定是只有在文件内容或者文件属性修改后的访问才会更新atime。对于老的内容查看是不需要记录atime
但是第二个条件,貌似不好理解为什么要时隔1天才会记录。
这个功能是commit 11ff6f05f1e836a6a0 提交的。
看来是我对atime的理解有问题,以为只要是读文件就会更新atime。不过如果一个文件系统需要严格的记录文件在什么时间被访问,那么现在linux上的文件系统都不能做到这一点。