Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is filename lookup done in ext4?

Env: Linux Kernel 5.3; FS: ext4

When requesting stat(const char *pathname, struct stat *statbuf) how is the const char *pathname is checked for existence?

It is necessary since in case there is no such path stat returns -1 (ENOENT). Here is the program I was testing:

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);
        }
    }
}

Every 2 iterations the file was deleted and re-created again on the next one. To inspect kernel call stack I used perf report:

enter image description here

The call stack does not meet my expectation. I expected ext4 calls under the vfs_statx in order to traverse ext4 internal data structures which would probably require disk I/O.

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);?

UPD: Taking closer look at the implementation I found that it seems to be taken from dentry cache as specified in the link_path_walk

like image 822
Some Name Avatar asked Sep 20 '25 04:09

Some Name


1 Answers

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);?

You should be able to do this through /proc/sys/vm/drop_caches (from 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

Basically just: echo 2 | sudo tee /proc/sys/vm/drop_caches.


As per the real question, in order to find how ext4 handles lookups, you can look at the inode_operations struct defined in fs/ext4/namei.c. More specifically, you are interested in the .lookup operation, which is ext4_lookup(). This function is called when doing lookups.

The call tree should be like this:

  • link_path_walk()
    • walk_component()
      • lookup_slow()
        • __lookup_slow()
          • inode->i_op->lookup(...) which for ext4 is ext4_lookup()
like image 56
Marco Bonelli Avatar answered Sep 22 '25 03:09

Marco Bonelli