一 概要
atime: last access time, 记录的是最后访问文件的时间戳,主要是read文件内容,不包括读取inode的内容,也就是说ls,stat都不会改变atime,因为它们只读取了inode的内容,并没有读取文件的内容。
mtime: last modified time,记录的是最后修改文件的时间戳,主要是write文件内容,不包括修改inode内容。
ctime: last status changing time,记录的是最后文件状态改变的时间戳,包括文件内容和inode内容修改,但不包括atime和mtime的修改,也就是说atime变了,ctime不一定会改变。
这是man 2 stat查看到的部分内容:
Not all of the Linux file systems implement all of the time fields. Some file system types allow mounting in such a
way that file and/or directory accesses do not cause an update of the st_atime field. (See noatime, nodiratime, and
relatime in mount(8), and related information in mount(2).) In addition, st_atime is not updated if a file is opened
with the O_NOATIME; see open(2).
The field st_atime is changed by file accesses, for example, by execve(2), mknod(2), pipe(2), utime(2) and read(2)
(of more than zero bytes). Other routines, like mmap(2), may or may not update st_atime.
The field st_mtime is changed by file modifications, for example, by mknod(2), truncate(2), utime(2) and write(2) (of
more than zero bytes). Moreover, st_mtime of a directory is changed by the creation or deletion of files in that
directory. The st_mtime field is not changed for changes in owner, group, hard link count, or mode.
The field st_ctime is changed by writing or by setting inode information (i.e., owner, group, link count, mode,
etc.).
从中可以得出如下信息: read大于0个字节时atime改变,write大于0个字节时mtime改变,也就是说以读写方式open文件时,所以的时间戳都不会改变,只有实际的读写操作时才会改变,记录的是最后操作的时间戳。
二 代码分析
分析内核代码:
do_generic_file_read()
{
...........
file_accessed(filp);
}
每次read后,都会调用file_accessed()更新atime.
__generic_file_aio_write()
{
...............
file_update_time();
}
每次write后,都会调用file_update_time()更新mtime和ctime,注意mtime改变时,ctime是一定会改变的。