linux字符设备驱动之open()函数

疑问:

  1. 用户的open()函数到驱动的open()函数的过程是怎么样的?
  2. 为什么传递了struct inode *inode, struct file *filp两个参数?

函数调用过程

调用过程

分析

在我们的驱动程序中会这样写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

  • 4
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值