示例2:创建一个configfs目录,用户空间可创建目录(新的内核对象)
首先是定义configfs子系统,
304static struct configfs_subsystem simple_children_subsys = {
305 .su_group = {
306 .cg_item = {
307 .ci_namebuf = "02-simple-children",
308 .ci_type = &simple_children_type,
309 },
310 },
311};
接着定义config_item_type,
289/*
290 * Note that, since no extra work is required on ->drop_item(),
291 * no ->drop_item() is provided.
292 */
293static struct configfs_group_operations simple_children_group_ops = {
294 .make_item = simple_children_make_item,
295};
296
297static struct config_item_type simple_children_type = {
298 .ct_item_ops = &simple_children_item_ops,
299 .ct_group_ops = &simple_children_group_ops,
300 .ct_attrs = simple_children_attrs,
301 .ct_owner = THIS_MODULE,
302};
298行,定义了子系统根目录属性操作,跟示例1类似
300行,定义了子系统根目录属性,跟示例1类似
299行,这里新增加了configfs_group_operations,具体定义在293行,继续跟进294行函数simple_children_make_item
232struct simple_children {
233 struct config_group group;
234};
235
236static inline struct simple_children *to_simple_children(struct config_item *item)
237{
238 return item ? container_of(to_config_group(item), struct simple_children, group) : NULL;
239}
240
241static struct config_item *simple_children_make_item(struct config_group *group, const char *name)
242{
243 struct simple_child *simple_child;
244
245 simple_child = kzalloc(sizeof(struct simple_child), GFP_KERNEL);
246 if (!simple_child)
247 return ERR_PTR(-ENOMEM);
248
249 config_item_init_type_name(&simple_child->item, name,
250 &simple_child_type);
251
252 simple_child->storeme = 0;
253
254 return &simple_child->item;
255}
241行,定义了configfs_group_operations->make_item操作,返回值是新创建的struct config_item,传入参数是struct config_group,即是指定config_group下创建config_item项,这就意味着在该子系统(i.e /sys/kernel/config/02-simple-children)下执行命令mkdir,最终会执行到这个函数。
那执行mkdir 之后,会生成一个怎样的新目录呢?
249行,当调用mkdir时,这时就调用config_item_init_type_name初始化这个config_item,接着看250行的simple_child_type到底定义了怎么一个item类型?
171static struct configfs_attribute simple_child_attr_storeme = {
172 .ca_owner = THIS_MODULE,
173 .ca_name = "storeme",
174 .ca_mode = S_IRUGO | S_IWUSR,
175};
176
177static struct configfs_attribute *simple_child_attrs[] = {
178 &simple_child_attr_storeme,
179 NULL,
180};
219static struct configfs_item_operations simple_child_item_ops = {
220 .release = simple_child_release,
221 .show_attribute = simple_child_attr_show,
222 .store_attribute = simple_child_attr_store,
223};
224
225static struct config_item_type simple_child_type = {
226 .ct_item_ops = &simple_child_item_ops,
227 .ct_attrs = simple_child_attrs,
228 .ct_owner = THIS_MODULE,
229};
225行,定义了config_item_type。
227行,先看属性,simple_child_attrs定义在177行,只有一个属性,这个属性定义在171行,仔细看一下这个属性,有可读写的。
所以经过mkdir之后,该目录下就自动生成一个名字为storeme的属性,其对应的操作定义在219行。具体如下:
# cd /sys/kernel/config/02-simple-children/
# ls
description
# mkdir test
# ll
-r--r--r--. 1 root root 4096 Sep 28 21:24 description
drwxr-xr-x. 2 root root 0 Sep 28 21:24 test
# tree
.
|-- description
`-- test
`-- storeme
1 directory, 2 files
# cat test/storeme
0
# echo 1 > test/storeme
# cat test/storeme
1
