代码之家  ›  专栏  ›  技术社区  ›  Some Name

在ext4中如何查找文件名?

  •  1
  • Some Name  · 技术社区  · 5 年前

    环境:Linux内核5.3;FS:ext4

    请求时 stat(const char *pathname, struct stat *statbuf) 怎么样 const char *pathname 是否检查存在?

    这是必要的,因为如果没有这样的路径 stat 退货 -1 (ENOENT) 。这是我测试的程序:

    static const char *pathname = "/some/fancy/path/name";
    
    int main(void){
        struct stat statbuf;
        unsigned long i = 0;
        int fd = -1;
        while(1){
            if((++i) % 2){
                fd = open(pathname, O_CREAT, 0644);
            }
            stat(pathname, &statbuf);
            if(i % 2){
                close(fd);
                unlink(pathname);
            }
        }
    }
    

    每两次迭代,文件都会被删除,并在下一次迭代中再次创建。检查我使用的内核调用堆栈 perf report :

    enter image description here

    调用堆栈不符合我的期望。我早料到 ext4 呼叫下 vfs_statx 为了穿越 分机4 可能需要磁盘I/O的内部数据结构。

    如果它被缓存在inode或dentry缓存中,如何刷新它以检查什么 分机4 电话需要 stat(const char *pathname, struct stat *statbuf); ?

    UPD:仔细查看实现,我发现它似乎是从dentry缓存中获取的,如 link_path_walk

    0 回复  |  直到 5 年前
        1
  •  4
  •   Marco Bonelli    5 年前

    如果它被缓存在inode或dentry缓存中,如何刷新它以检查什么 ext4 电话需要 stat(const char *pathname, struct stat *statbuf); ?

    你应该能够做到这一点 /proc/sys/vm/drop_caches (从 Documentation/sysctl/vm.txt ):

    drop_caches
    
    Writing to this will cause the kernel to drop clean caches, as well as
    reclaimable slab objects like dentries and inodes.  Once dropped, their
    memory becomes free.
    
    To free pagecache:
      echo 1 > /proc/sys/vm/drop_caches
    To free reclaimable slab objects (includes dentries and inodes):
      echo 2 > /proc/sys/vm/drop_caches
    To free slab objects and pagecache:
      echo 3 > /proc/sys/vm/drop_caches
    

    基本上只是: echo 2 | sudo tee /proc/sys/vm/drop_caches .


    根据实际问题,为了了解ext4如何处理查找,您可以查看 inode_operations 结构 defined in fs/ext4/namei.c 更具体地说,您对 .lookup 操作,即 ext4_lookup() 。执行查找时调用此函数。

    调用树应该是这样的: