gpt4 book ai didi

c - 如何在 ext4 中查找文件名?

转载 作者:行者123 更新时间:2023-12-03 09:49:06 28 4
gpt4 key购买 nike

环境:Linux 内核 5.3;全系统: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);
}
}
}

每 2 次迭代,文件就会被删除并在下一次迭代时重新创建。为了检查内核调用堆栈,我使用了 perf report:

enter image description here

调用堆栈不符合我的预期。我希望在 vfs_statx 下调用 ext4为了遍历可能需要磁盘 I/O 的 ext4 内部数据结构。

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

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

最佳答案

If it was cached in the inode or dentry cache how to flush it in order to inspect what ext4 calls would require 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() .查找时会调用此函数。

调用树应该是这样的:

关于c - 如何在 ext4 中查找文件名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61624142/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com