Get all mount points in kernel module

老子叫甜甜 提交于 2021-02-08 07:23:30

问题


I'm trying to get all the mount points in a kernel module. Below is what I've come up with. It segfaults because of the strcat. Is this the correct way to get the mount points? Will this work? if so how do i fix the segfault? If not, how does one go about getting the mount points in a linux kernel module?

I've tried cycle the whole namespace looking for mountpoint roots that match but its from 2003 and the kernel has changed so much so its basically useless. Also tried get filesystem mount point in kernel module but again its from 2012 so it to is outdated.

static int __init misc_init(void)
{
    struct path path;
    struct dentry *thedentry;
    struct dentry *curdentry;

    kern_path("/", LOOKUP_FOLLOW, &path);
    thedentry = path.dentry;
    list_for_each_entry(curdentry, &thedentry->d_subdirs, d_child)
    {
        kern_path(strncat("/", curdentry->d_name.name, strlen(curdentry->d_name.name)), LOOKUP_FOLLOW, &path);
        if (path_is_mountpoint(&path))
        {
            printk("%s: is a mountpoint", curdentry->d_name.name);
        }
        else
            printk("%s: is not a mountpoint", curdentry->d_name.name);
    }
    return 0;
}

回答1:


There are flags for it in the dentry struct. d_flags. and there is a flag DCACHED_MOUNTED. Get the current pointer. The fs_struct in there. then the root. this gives you the root of the current filesystem. from there loop though all the subdirs and if d_flags & DCACHE_MOUNTED passes then it is a mount point.

ssize_t read_proc(struct file *filp, char *buf, size_t len, loff_t *offp )
{
    struct dentry *curdentry;

    list_for_each_entry(curdentry, &current->fs->root.mnt->mnt_root->d_subdirs, d_child)
    {
        if ( curdentry->d_flags & DCACHE_MOUNTED)
            printk("%s is mounted", curdentry->d_name.name);
    }
    return 0;
}



回答2:


May be the following is more optimal way to get all mountpoints w/o checking all dentries of a system.

struct mnt_namespace *ns = current->nsproxy->mnt_ns;
struct mount *mnt;

list_for_each_entry(mnt, &ns->list, mnt_list) {
...do something with each mnt...
}

Note this code doesn't hold namespace_sem so the result of iterating thru mnt_list is not guarantee. However, IMHO it is as minimum not less correct than traversing all dentries w/o holding mount lock.



来源:https://stackoverflow.com/questions/46591203/get-all-mount-points-in-kernel-module

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!