I want to implement a tree traversal function which prints all the contents of a given directory in kernel . I know how to do this in user space, but my requirement is to have that in kernel space. For that, I am looking into vfs_readdir function and a bit confused regarding its usage. Say I will be calling my traversal function from other kernel module which means that request won’t be coming through user space. Now the question is how to call vfs_readdir and use that information to recursively parse given directory. From definition of vfs_readdir extern int vfs_readdir(struct file *, filldir_t, void *);
I can get struct file * from the file path using functions like filp_open() and as per my understanding filldir_t is a function pointer to call back function which fills the user provided buffer pointed by void *. But in my case, I don’t need to pass any information to user back. What should i pass in void * place? Looking into filldir function definition
static int filldir(void * __buf, const char * name, int namlen, loff_t offset, ino_t ino, unsigned int d_type);
From where are the parameters to this function are coming. My assumption is that vfs_readdir in turn calls something like file->f_op_readdir(file,but,filler); Does this internally does something and fills in parameters to call callback function ? Now this is one level. What should I do to recursively print all files in a given directory. I guess I need to do something in my own callback function.But I have only some information regarding the file that are passed through this call back functions like name of file,inode number etc . How would I know if it is regular file or directory using this information . I mean I don't have dentry or inode data structures regarding the file. Any suggestions how to do that? In addition, If I want to delete a file in callback function, can I do that by using inode numbers(thats what i have in callback apart from names) how should I do that?