创建一个最简单的configfs目录和可读写的属性
第一个例子是一个无子子系统。它不能创建任何config_items。它只有属性。
创建目录01-childless,下面有三个属性,其中属性storeme为可写,结果如下:
# ll /sys/kernel/config/01-childless/
-r--r--r--. 1 root root 4096 Sep 27 05:16 description
-r--r--r--. 1 root root 4096 Sep 27 05:16 showme
-rw-r--r--. 1 root root 4096 Sep 27 05:16 storeme
/*创建一个结构体包含了configfs子系统,两个属性showme, storeme放在了这里*/
struct childless {
struct configfs_subsystem subsys;
int showme;
int storeme;
};
#define CONFIGFS_ATTR(_pfx, _name) \
static struct configfs_attribute _pfx##attr_##_name = { \
.ca_name = __stringify(_name), \
.ca_mode = S_IRUGO | S_IWUSR, \
.ca_owner = THIS_MODULE, \
.show = _pfx##_name##_show, \
.store = _pfx##_name##_store, \
}
#define CONFIGFS_ATTR_RO(_pfx, _name) \
static struct configfs_attribute _pfx##attr_##_name = { \
.ca_name = __stringify(_name), \
.ca_mode = S_IRUGO, \
.ca_owner = THIS_MODULE, \
.show = _pfx##_name##_show, \
}
#define CONFIGFS_ATTR_WO(_pfx, _name) \
static struct configfs_attribute _pfx##attr_##_name = { \
.ca_name = __stringify(_name), \
.ca_mode = S_IWUSR, \
.ca_owner = THIS_MODULE, \
.store = _pfx##_name##_store, \
}
/*属性定义如下:*/
CHILDLESS_ATTR_RO(showme, childless_showme_read);
CHILDLESS_ATTR(storeme, S_IRUGO | S_IWUSR, childless_storeme_read, childless_storeme_write);
CHILDLESS_ATTR_RO(description, childless_description_read);
static struct configfs_attribute *childless_attrs[] = {
&childless_attr_showme.attr, // 由CHILDLESS_ATTR_RO(showme, childless_showme_read)定义
&childless_attr_storeme.attr, // 由CHILDLESS_ATTR(storeme, S_IRUGO | S_IWUSR, childless_storeme_read, childless_storeme_write);定义
&childless_attr_description.attr, // 由CHILDLESS_ATTR_RO(description, childless_description_read);
NULL,
};
/*属性的操作定义如下:*/
CONFIGFS_ATTR_OPS(childless);
static struct configfs_item_operations childless_item_ops = {
.show_attribute = childless_attr_show, // 由CONFIGFS_ATTR_OPS(childless)定义
.store_attribute = childless_attr_store, // 由CONFIGFS_ATTR_OPS(childless)定义
};
/*属性和属性操作定义*/
static struct config_item_type childless_type = {
.ct_item_ops = &childless_item_ops,
.ct_attrs = childless_attrs,
.ct_owner = THIS_MODULE,
};
/*子系统定义如下*/
static struct childless childless_subsys = {
.subsys = {
.su_group = {
.cg_item = {
.ci_namebuf = "01-childless", // 定义目录名称
.ci_type = &childless_type, // 定义属性和操作
},
},
},
};
/*一切准备就绪,在模块初始化地方调用子系统注册函数,然后就可以在/sys/kernel/config/下看到创建了子系统了*/
static int __init configfs_childless_init(void)
{
int ret;
int i;
struct configfs_subsystem *subsys;
for (i = 0; childless_subsys[i]; i++) {
subsys = childless_subsys[i];
config_group_init(&subsys->su_group); // 初始化子系统
mutex_init(&subsys->su_mutex); // 初始化子系统
ret = configfs_register_subsystem(subsys); // 注册子系统
if (ret) {
printk(KERN_ERR "Error %d while registering subsystem %s\n",ret,subsys->su_group.cg_item.ci_namebuf);
goto out_unregister;
}
}
return 0;
}
小结:
1、创建子系统struct configfs_subsystem
2、创建子系统下config_item_type,对应位置configfs_subsystem->config_group->config_item->config_item_type
3、创建config_item_type对应的属性数组和操作,操作主要是show_attribute和store_attribute
4、注册子系统configfs_register_subsystem