如何在内核中获取字符设备的节点名或路径

How to get the node name or path of the character device in kernel

我在一个内核驱动程序中有 "n" 个字符设备。一个读函数是指读指针。

static struct file_operations fops;
fops.read    = cd_read;

现在我需要知道当从用户空间调用 read 时引用了哪个字符设备。

static ssize_t cd_read(struct file *filep, char *buffer, size_t len, loff_t *position)
{

    filep->f_path;
}

我试图通过 filep->f_path 得到它至少尝试打印它但是 f_path指的是fs.h

中的struct path
struct file {
    ...
    struct path     f_path;
    ...
}

path.h 路径中的 dentry 和 vfsmount 引用了 2 个未定义的结构。

struct dentry;
struct vfsmount;

struct path {
    struct vfsmount *mnt;
    struct dentry *dentry;
};

然后卡在这里。 那么如何获取内核中字符设备的节点名或路径呢?

我找到了解决方案。

filp->f_path.dentry->d_iname

按照此处所述工作:

In Linux, how can I get the filename from the "struct file" structure, while stepping thru the kernel with kgdb?