In Ubuntu 22+, How to list directory in kernel module by C language?

In Ubuntu 20, I use the demo code in attachment “hello.c” to show the files in a directory, but when I upgraded to Ubuntu 22.04(Linux kernel version 6.8.0-49), it doesn’t work, I found the structure definition of dentry in dcache.h changed, and the demo compilation failed, so how can I list a directory in Ubuntu 22+ in the kernel?


Makefile

Your question is very advanced and it might be difficult to get help here.
I do observe that you use dentry->d_subdirs and some others that have been renamed, and potentially changed in what they do.

OLD:

        };
        struct list_head d_child;       /* child of parent list */
        struct list_head d_subdirs;     /* our children */

NEW:

        };
        struct hlist_node d_sib;        /* child of parent list */
        struct hlist_head d_children;   /* our children */

You would have to find and look at the commits that made the changes to understand if your code can be made to work again.

2 Likes

Thanks for your tips, after changed list_for_each() to hlist_for_each(), I finally get the demo works in Ubuntu 22.04. In case other guys have the same issue, I post the whole demo here.