95 源码分析:如何保存file_operation接口

本文深入解析了Linux字符设备驱动中的关键数据结构,如struct cdev和哈希表cdev_map->probes,并详细分析了cdev_init及cdev_add等核心函数的工作原理。

一、关键数据结构梳理

1、struct cdev

struct cdev 结构体:专门用来管理字符设备,定义在:kernel/ebf-buster-linux/include/linux/cdev.h

字符设备管理对象

struct cdev {
	//内核驱动基本对象,让字符设备继承内核基本驱动对象
    struct kobject kobj;
	//相关内核模块
    struct module *owner;
	//设备驱动接口
    const struct file_operations *ops;
	//链表节点,暂不关注
    struct list_head list;
	//设备号,32位
    dev_t dev;
	//次设备号的数量
    unsigned int count;

} __randomize_layout;

2、哈希表 cdev_map->probes

哈希表 cdev_map->probes :定义在 ebf-buster-linux/fs/char_dev.c
此哈希表专门用来管理 file_operations 接口。
当需要查找 文件的操作接口 时,根据 设备号 来查找 哈希表cdev_map->probes ,找到 对应的 struct probe 后,找到其 成员变量 data,其指向 struct cdev ,最终找到 file_operations

其数据类型:struct kobj_map 定义在 map.c

struct kobj_map {
	struct probe {
		//指向下一个链表节点
		struct probe *next;
		//设备号
		dev_t dev;
		//次设备号的数量
		unsigned long range;
		struct module *owner;
		kobj_probe_t *get;
		int (*lock)(dev_t, void *);
		//空指针,内核常用技巧,此处用来指向 struct cdev
		void *data;
	} *probes[255];
	struct mutex *lock;
};

二、cdev_init 函数分析

此函数定义在:ebf-buster-linux/fs/char_dev.c
主要操作是:保存file_operation到cdev中。

/**
 * cdev_init() - initialize a cdev structure
 * @cdev: the structure to initialize
 * @fops: the file_operations for this device
 *
 * Initializes @cdev, remembering @fops, making it ready to add to the
 * system with cdev_add().
 */
void cdev_init(struct cdev *cdev, const struct file_operations *fops)
{
	memset(cdev, 0, sizeof *cdev);
	INIT_LIST_HEAD(&cdev->list);
	kobject_init(&cdev->kobj, &ktype_cdev_default);
	/* 保存file_operation到cdev中。 */
	cdev->ops = fops;
}

三、cdev_add函数分析

此函数定义在:ebf-buster-linux/fs/char_dev.c
主要操作:根据哈希函数保存struct cdevcdev_map->probes 哈希表 中,方便内核查找file_operation使用。

/**
 * cdev_add() - add a char device to the system
 * @p: the cdev structure for the device
 * @dev: the first device number for which this device is responsible
 * @count: the number of consecutive minor numbers corresponding to this
 *         device
 *
 * cdev_add() adds the device represented by @p to the system, making it
 * live immediately.  A negative error code is returned on failure.
 */
int cdev_add(struct cdev *p, dev_t dev, unsigned count)
{
	int error;
	/* 保存设备号到 cdev */
	p->dev = dev;
	/* 保存次设备号的数量 */
	p->count = count;
	/* 把 cdev 填充到哈希表 cdev_map->probes 中的合适位置 */
	error = kobj_map(cdev_map, dev, count, NULL,
			 exact_match, exact_lock, p);
	if (error)
		return error;

	kobject_get(p->kobj.parent);

	return 0;
}
int kobj_map(struct kobj_map *domain, dev_t dev, unsigned long range,
	     struct module *module, kobj_probe_t *probe,
	     int (*lock)(dev_t, void *), void *data)
{
	/* 判断次设备号的数量是否溢出,若溢出了需要再分配一个主设备号。 
	 * 这一步其实就是判断:需要几个主设备号。
	 */
	unsigned n = MAJOR(dev + range - 1) - MAJOR(dev) + 1;
	/* 获取主设备号 */
	unsigned index = MAJOR(dev);
	unsigned i;
	struct probe *p;
	/* n为需要主设备号的数量 */
	if (n > 255)
		n = 255;
	/* 动态申请内存:n个probe结构体大小 */
	p = kmalloc_array(n, sizeof(struct probe), GFP_KERNEL);
	if (p == NULL)
		return -ENOMEM;
	/* n一般为1,此循环一般只会执行一次,
	 * 把相关参数保存到 struct probe 结构体
	 */
	for (i = 0; i < n; i++, p++) {
		p->owner = module;
		p->get = probe;
		p->lock = lock;
		/* 设备号 */
		p->dev = dev;
		/* 次设备号的数量 */
		p->range = range;
		/* 保存 cdev 对象 */
		p->data = data;
	}
	mutex_lock(domain->lock);
	/* 将上面制作成的 struct probe 结构体保存到哈希表 cdev_map->probes 
	 * 此时不需要判断次设备号的合法性,因为分配的时候已经判断过了。
	 */
	for (i = 0, p -= n; i < n; i++, p++, index++) {
		struct probe **s = &domain->probes[index % 255];
		while (*s && (*s)->range < range)
			s = &(*s)->next;
		p->next = *s;
		*s = p;
	}
	mutex_unlock(domain->lock);
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值