疑问:
在我们的驱动程序中会这样写open()函数
static int imxirq_open(struct inode *inode, struct file *filp)
{
filp->private_data = &imxirq; /* 设置私有数据 */
return 0;
}
而在我们的应用程序中会这样写,其中的filename是一个具体的路径名,比如"/dev/key"
fd = open(filename, O_RDWR);
if (fd < 0) {
printf("Can't open file %s\r\n", filename);
return -1;
}
用户空间的open()函数会调用到这个函数
232 struct file *filp_open(const char *filename, int flags, umode_t mode)
233 {
234 struct filename *name = getname_kernel(filename);
235 struct file *file = ERR_CAST(name);
236
237 if (!IS_ERR(name)) {
238 file = file_open_name(name, flags, mode);
239 putname(name);
240 }
241 return file;
}
会依次调用到下面的函数,在下面的函数中会调用d_backing_inode()将path转换成inode。
int vfs_open(const struct path *path, struct file *file,
const struct cred *cred)
{
struct dentry *dentry = d_real(path->dentry, NULL, file->f_flags, 0);
if (IS_ERR(dentry))
return PTR_ERR(dentry);
file->f_path = *path;
return do_dentry_open(file, d_backing_inode(dentry), NULL, cred);
}
先看下struct dentry结构体的内容:在其中包含有inode。
struct dentry {
...
struct inode *d_inode; /* Where the name belongs to - NULL is negative */
unsigned char d_iname[DNAME_INLINE_LEN]; /* small names */
...
} __randomize_layout;
最后在下面的函数中会调用我们驱动中的open()函数
static int do_dentry_open(struct file *f,
struct inode *inode,
int (*open)(struct inode *, struct file *),
const struct cred *cred)
{
...
if (!open)
open = f->f_op->open;
if (open) {
error = open(inode, f); //这个就是我们驱动的open函数
if (error)
goto cleanup_all;
}
...
}
从上面的函数中可以看出,函数调用的过程中会将我们在用户空间使用的设备地址转换成具体的inode。struct file是整个函数调用将返回的函数指针。
PS:内核版本为linux_4.14.98