configfs 示例

创建一个configfs目录,用户空间可创建目录(新的内核对象)

/*定义configfs子系统*/
static struct configfs_subsystem simple_children_subsys = {
    .su_group = {
          .cg_item = {
              .ci_namebuf = "02-simple-children",
              .ci_type = &simple_children_type,
          },
    },
};


/*接着定义config_item_type*/
static struct config_item_type simple_children_type = {
    .ct_item_ops    = &simple_children_item_ops,                 //定义了子系统根目录属性操作
    .ct_group_ops    = &simple_children_group_ops,               //定义了子系统容器属性操作
    .ct_attrs    = simple_children_attrs,                       //定义了子系统根目录属性
    .ct_owner    = THIS_MODULE,
};

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


struct simple_children {
    struct config_group group;
};

static inline struct simple_children *to_simple_children(struct config_item *item)
{
    return item ? container_of(to_config_group(item), struct simple_children, group) : NULL;
}

/*定义了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 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,
};

  • 6
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值