configfs 示例3

创建一个最简单的configfs目录和可读写的属性,用户空间可以创建目录

例子是子系统能创建任何config_items,有属性。

创建目录01-childless,下面有三个属性,其中属性storeme为可写,结果如下:
# 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


/*创建一个结构体包含了configfs子系统,两个属性showme, storeme放在了这里*/
struct children {
    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, children_showme_read);
CHILDLESS_ATTR(storeme, S_IRUGO | S_IWUSR, children_storeme_read, children_storeme_write);
CHILDLESS_ATTR_RO(description, children_description_read);

static struct configfs_attribute *children_attrs[] = {
    &children_attr_showme.attr,                    // 由CHILDLESS_ATTR_RO(showme, childless_showme_read)定义
    &children_attr_storeme.attr,                   // 由CHILDLESS_ATTR(storeme, S_IRUGO | S_IWUSR, childless_storeme_read, childless_storeme_write);定义
    &children_attr_description.attr,               // 由CHILDLESS_ATTR_RO(description, childless_description_read);
    NULL,
};

/*属性的操作定义如下:*/
CONFIGFS_ATTR_OPS(childless);
static struct configfs_item_operations children_item_ops = {
    .show_attribute          = children_attr_show,    // 由CONFIGFS_ATTR_OPS(childless)定义
    .store_attribute    = children_attr_store,        // 由CONFIGFS_ATTR_OPS(childless)定义
};


static struct configfs_attribute simple_child_attr_storeme = {
 .ca_owner = THIS_MODULE,
 .ca_name = "storeme",
 .ca_mode = S_IRUGO | S_IWUSR,
};

static struct configfs_attribute *simple_child_attrs[] = {
 &simple_child_attr_storeme,
 NULL,
};
static struct configfs_item_operations simple_child_item_ops = {
 .release = simple_child_release,
 .show_attribute = simple_child_attr_show,
 .store_attribute = simple_child_attr_store,
};

static struct config_item_type simple_child_type = {
 .ct_item_ops = &simple_child_item_ops,
 .ct_attrs = simple_child_attrs,
 .ct_owner = THIS_MODULE,
};


/*定义了configfs_group_operations->make_item操作,返回值是新创建的struct config_item,传入参数是struct config_group,即是指定config_group下创建config_item项,这就意味着在该子系统(i.e /sys/kernel/config/02-simple-children)下执行命令mkdir,最终会执行到这个函数*/
static struct config_item *simple_children_make_item(struct config_group *group, const char *name)
{
    struct simple_child *simple_child;

    simple_child = kzalloc(sizeof(struct simple_child), GFP_KERNEL);
    if (!simple_child)
          return ERR_PTR(-ENOMEM);

    //当调用mkdir时,这时就调用config_item_init_type_name初始化这个config_item
    config_item_init_type_name(&simple_child->item, name, &simple_child_type);    
    simple_child->storeme = 0;
    
    return &simple_child->item;
}


static struct config_g
roup *group_children_make_group(struct config_group *group, const char *name)
{
    struct simple_children *simple_children;

    simple_children = kzalloc(sizeof(struct simple_children),
                      GFP_KERNEL);
    if (!simple_children)
          return ERR_PTR(-ENOMEM);

    config_group_init_type_name(&simple_children->group, name,
                        &simple_children_type);

    return &simple_children->group;
}

/*接着定义容器的操作 configfs_group_operations */
static struct configfs_group_operations children_group_ops= {
    .make_item    = simple_children_make_item,
    .make_group    = group_children_make_group,
};


/*属性和属性操作定义*/
static struct config_item_type children_type = {
    .ct_item_ops    = &children_item_ops,
    .ct_group_ops    = &children_group_ops,    
    .ct_attrs    = children_attrs,
    .ct_owner    = THIS_MODULE,
};

/*子系统定义如下*/
static struct childless children_subsys = {
    .subsys = {
          .su_group = {
              .cg_item = {
                    .ci_namebuf = "02-children",       // 定义目录名称
                    .ci_type = &children_type,         // 定义属性和操作
              },
          },
    },
};


/*一切准备就绪,在模块初始化地方调用子系统注册函数,然后就可以在/sys/kernel/config/下看到创建了子系统了*/

static int __init configfs_children_init(void)
{
    int ret;
    int i;
    struct configfs_subsystem *subsys;

    for (i = 0; children_subsys[i]; i++) {
          subsys = children_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
 

  • 5
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值