Moi*_*ýni 6 c linux linux-kernel
任何人都可以提供代码来克服这个问题吗?
有效地我们如何struct inode*从文件中获取内核级别/dev/driver1?
在用户空间中给出:
int fd;
fd = open("/dev/driver1", O_RDWR | O_SYNC);
Run Code Online (Sandbox Code Playgroud)
在内核空间:
static long dev_ioctl(struct file *file, unsigned cmd, unsigned long arg)
struct dev_handle *handle;
handle = file->private_data;
Run Code Online (Sandbox Code Playgroud)
假设,我们不走那条路,
我们如何通过例如内核本身获得.难以编码file->private_data处理?
您正在寻找filp_open功能。从文件include/linux/fs.h:
struct file *filp_open(const char *filename, int flags, umode_t mode);
Run Code Online (Sandbox Code Playgroud)
以下是函数源和文档的链接:http://lxr.free-electrons.com/source/fs/open.c#L937
如果你确实需要 FD,你可以使用sys_open(在较新的内核中不导出):
long sys_open(const char __user *filename, int flags, int mode);
Run Code Online (Sandbox Code Playgroud)
您可以在类似的问题上找到一个非常好的答案: How to read/write files inside a Linux kernel module?
编辑(如何获取inode):
您可以inode从以下位置获取缓存struct file:
struct file *file = ...;
struct inode *inode = file->inode;
Run Code Online (Sandbox Code Playgroud)
如果你想要锁定:这里是背景:Documentation/filesystems/path-lookup.txt
遍历的起点是current->fs->root。内核中有许多函数已经完成了这项工作,您可以在fs/namei.c源文件中找到它们。
有一个函数:kern_path:
int error;
struct inode *inode;
struct path path;
error = kern_path(pathname, LOOKUP_FOLLOW, &path);
if (error) ...;
inode = path.dentry->d_inode;
Run Code Online (Sandbox Code Playgroud)