sysfs - 用于导出内核对象(kobject)的文件系统
sysfs - _The_ filesystem for exporting kernel objects.
sysfs - 用于导出内核对象(kobject)的文件系统
Patrick Mochel <mochel@osdl.org>
翻译 : tekkamanninja <tekkamanninja@163.com>
10 January 2003
2003年1月10日
翻译时间:2007年12月29日
What it is:
简介:
~~~~~~~~~~~
sysfs is a ram-based filesystem initially based on ramfs. It provides
sysfs 是一个最初基于ramfs的位于内存的文件系统。它提供
a means to export kernel data structures, their attributes, and the
一些方法以导出内核的数据结构、他们的属性和
linkages between them to userspace.
他们与用户空间的连接。
sysfs is tied inherently to the kobject infrastructure. Please read
sysfs 始终与kobject的底层结构紧密相关。请阅读
Documentation/kobject.txt for more information concerning the kobject
Documentation/kobject.txt 文档以获得更多关于 kobject 接口的信息。
interface.
Using sysfs
使用
~~~~~~~~~~~
sysfs is always compiled in. You can access it by doing:
sysfs 通常被编译进内核。你可以通过使用以下命令访问它:
mount -t sysfs sysfs /sys
(此命令含义是挂载 sysfs 到根目录下的sys目录)
Directory Creation
创建目录
~~~~~~~~~~~~~~~~~~
For every kobject that is registered with the system, a directory is
一旦有 kobject 在系统中注册,就会有一个目录在sysfs中被创建。
created for it in sysfs. That directory is created as a subdirectory 这个目录是作为 kobject 的 parent 下的子目录创建的,
of the kobject's parent, expressing internal object hierarchies to
以准确的传递内核的对象层次到 userspace. Top-level directories in sysfs represent the common
用户空间。 sysfs中的顶层目录代表着内核对象层次的共同祖先;
ancestors of object hierarchies; i.e. the subsystems the objects
例如:某些对象属于某个子系统。 belong to.
Sysfs internally stores the kobject that owns the directory in the
Sysfs内部存储着 kobject ,这些 kobject 在 d_fsdata 指针(在 kobject
->d_fsdata pointer of the directory's dentry. This allows sysfs to do
的dentry结构体中)中拥有目录。 这使得 sysfs 可以在文件 reference counting directly on the kobject when the file is opened and
打开和关闭时,直接在 kobject 上实现引用计数。
closed.
Attributes
属性
~~~~~~~~~~
Attributes can be exported for kobjects in the form of regular files in
kobject 的属性能在文件系统中以普通文件的形式导出。
the filesystem. Sysfs forwards file I/O operations to methods defined
Sysfs 为属性定义了面向文件 I/O 操作的方法,
for the attributes, providing a means to read and write kernel
以提供对内核属性的读写。 attributes.
Attributes should be ASCII text files, preferably with only one value
属性应为 ASCII 码文本文件,以一个文件只存储一个属性值为宜。
per file. It is noted that it may not be efficient to contain only
但一个文件只包含一个属性值可能影响效率, value per file, so it is socially acceptable to express an array of
所以一个包含相同数据类型的属性值数组也是被广泛接受的。 values of the same type.
Mixing types, expressing multiple lines of data, and doing fancy
混合类型、表达多行数据以及一些怪异的数据格式是会遭强烈反对。
formatting of data is heavily frowned upon. Doing these things may get
这样做是很丢脸的,而且 you publically humiliated and your code rewritten without notice.
你的代码会在未通知你的情况下被重写。
An attribute definition is simply:一个简单的属性结构定义如下:(到2.6.22.2已添加了struct module * owner;)
struct attribute {
char * name;
mode_t mode;
};
int sysfs_create_file(struct kobject * kobj, struct attribute * attr);
void sysfs_remove_file(struct kobject * kobj, struct attribute * attr);
A bare attribute contains no means to read or write the value of the
一个裸的属性并不包含读写其属性值的方法。
attribute. Subsystems are encouraged to define their own attribute
最好为子系统定义自己的属性 structure and wrapper functions for adding and removing attributes for
和 为了增删特殊对象类型的属性而 包装过的函数。 a specific object type.
For example, the driver model defines struct device_attribute like:
例如:驱动程序模型定义的device_attribute 结构体如下:
struct device_attribute {
struct attribute attr;
ssize_t (*show)(struct device * dev, char * buf);
ssize_t (*store)(struct device * dev, const char * buf);
};
int device_create_file(struct device *, struct device_attribute *);
void device_remove_file(struct device *, struct device_attribute *);
It also defines this helper for defining device attributes:
它为了定义设备的属性也定义了辅助的宏:
#define DEVICE_ATTR(_name, _mode, _show, _store) \
struct device_attribute dev_attr_##_name = { \
.attr = {.name = stringify(_name) , .mode = _mode }, \
.show = _show, \
.store = _store, \
};
For example, declaring
例如:声明static DEVICE_ATTR(foo, S_IWUSR | S_IRUGO, show_foo, store_foo);
is equivalent to doing:
等同于这样的代码
static struct device_attribute dev_attr_foo = {
.attr = {
.name = "foo",
.mode = S_IWUSR | S_IRUGO,
},
.show = show_foo,
.store = store_foo,
};
Subsystem-Specific Callbacks
子系统特有的调用
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
When a subsystem defines a new attribute type, it must implement a
当一个子系统定义一个新属性类型时,
set of sysfs operations for forwarding read and write calls to the
一系列的sysfs操作必须被执行,以帮助读写函数实现
show and store methods of the attribute owners.
属性所有者的显示和储存的方法。
struct sysfs_ops {
ssize_t (*show)(struct kobject *, struct attribute *, char *);
ssize_t (*store)(struct kobject *, struct attribute *, const char *);
};
[ Subsystems should have already defined a struct kobj_type as a
[子系统应已经定义了一个kobj_type 结构体作为
descriptor for this type, which is where the sysfs_ops pointer is
这个类型的描述符,存储 sysfs_ops 的指针。
stored. See the kobject documentation for more information. ]
更多的信息参见 kobject 的文档 ]
When a file is read or written, sysfs calls the appropriate method
当一个文件被读写时, sysfs 会为这个类型调用适当的方法。
for the type. The method then translates the generic struct kobject
这个方法会将一般的 kobject 和 attribute 结构体指针 转换为 and struct attribute pointers to the appropriate pointer types, and
适当的指针类型后calls the associated methods.
调用相关联的函数。
To illustrate:
示例:
#define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
#define to_dev(d) container_of(d, struct device, kobj)
static ssize_t
dev_attr_show(struct kobject * kobj, struct attribute * attr, char * buf)
{
struct device_attribute * dev_attr = to_dev_attr(attr);
struct device * dev = to_dev(kobj);
ssize_t ret = 0;
if (dev_attr->show)
ret = dev_attr->show(dev, buf);
return ret;
}
Reading/Writing Attribute Data
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
读写属性数据
To read or write attributes, show() or store() methods must be
在声明属性时,show() 或 store() 方法必须被指明,以实现属性的读或写。
specified when declaring the attribute. The method types should be as
这些方法的类型应该和以下的设备属性 simple as those defined for device attributes:
的定义一样简单。
ssize_t (*show)(struct device * dev, char * buf);
ssize_t (*store)(struct device * dev, const char * buf);
IOW, they should take only an object and a buffer as parameters.
也就是说,他们应该只以一个处理对象和一个缓冲指针作为参数。
sysfs allocates a buffer of size (PAGE_SIZE) and passes it to the
sysfs 会分配一个缓冲区的大小(PAGE_SIZE)并传递给这个方法。
method. Sysfs will call the method exactly once for each read or
Sysfs 将会为每次读写操作调用一次这个方法。write. This forces the following behavior on the method
这导致了这些方法的执行会出现以下的行为: implementations:
- On read(2), the show() method should fill the entire buffer.
-在读方面,show() 方法应该填充整个缓冲区。
Recall that an attribute should only be exporting one value, or an
回想起属性应只导出了一个属性值或是一个同类型的属性值的数组, array of similar values, so this shouldn't be that expensive.
所以这个代价将不会不太高。
This allows userspace to do partial reads and seeks arbitrarily over
这使得用户空间可以局部地读和任意的搜索整个文件。 the entire file at will.
- On write(2), sysfs expects the entire buffer to be passed during the
-在些方面,sysfs 希望在第一次写操作时得到整个缓冲区。 first write. Sysfs then passes the entire buffer to the store()
之后 Sysfs 传递整个缓冲区给 store()方法。
method.
When writing sysfs files, userspace processes should first read the
当要写 sysfs 文件时,用户空间进程应该首先读整个文件,
entire file, modify the values it wishes to change, then write the
修该想要改变的值,然后回写整个缓冲区。 entire buffer back.
Attribute method implementations should operate on an identical
在读写属性值时,属性方法的执行应操作相同的缓冲区。 buffer when reading and writing values.
Other notes:
注记:
- The buffer will always be PAGE_SIZE bytes in length. On i386, this
is 4096.
- 缓冲区应总是 PAGE_SIZE 大小。对于i386,这个值为4096。
- show() methods should return the number of bytes printed into the
buffer. This is the return value of snprintf().
- show() 方法应该返回写入缓冲区的字节数,也就是 snprintf()的返回值。
- show() should always use snprintf().
- show() 应始终使用 snprintf()。 - store() should return the number of bytes used from the buffer. This
can be done using strlen().
- store() 应返回缓冲区的已用字节数,可使用 strlen()。
- show() or store() can always return errors. If a bad value comes
through, be sure to return an error.
- show() 或 store() 可以返回错误值。当得到一个非法值,必须返回一个错误值。
- The object passed to the methods will be pinned in memory via sysfs
referencing counting its embedded object. However, the physical
entity (e.g. device) the object represents may not be present. Be
sure to have a way to check this, if necessary.
- 一个传递给方法的对象将会通过 sysfs 调用对象内嵌的引用计数固定在内存中。尽管如此,对象代表的物
理实体(如设备)可能已不存在。如有必要,应该实现一个检测机制。
A very simple (and naive) implementation of a device attribute is:
一个简单的(未经实验证实的)设备属性例程如下:
static ssize_t show_name(struct device *dev, struct device_attribute *attr, char *buf)
{
return snprintf(buf, PAGE_SIZE, "%s\n", dev->name);
}
static ssize_t store_name(struct device * dev, const char * buf)
{
sscanf(buf, "%20s", dev->name);
return strnlen(buf, PAGE_SIZE);
}
static DEVICE_ATTR(name, S_IRUGO, show_name, store_name);
(Note that the real implementation doesn't allow userspace to set the
name for a device.)
(注意:真实的程序不允许用户空间设置设备名。)
Top Level Directory Layout
~~~~~~~~~~~~~~~~~~~~~~~~~~
顶层目录
The sysfs directory arrangement exposes the relationship of kernel
sysfs 目录的安排显示了内核数据结构之间的关系。
data structures.
The top level sysfs directory looks like:顶层 sysfs 目录如下:
block/
bus/
class/
devices/
firmware/
net/
fs/
devices/ contains a filesystem representation of the device tree. It maps
devices/ 包含了一个设备树的文件系统表示。 他直接 directly to the internal kernel device tree, which is a hierarchy of
以内核设备树的形式反映了设备的层次结构。
struct device.
bus/ contains flat directory layout of the various bus types in the
bus/ 包含了各种内核总线类型的固定目录布局。
kernel. Each bus's directory contains two subdirectories:
每个总线目录包含两个子目录:
devices/
drivers/
devices/ contains symlinks for each device discovered in the system
devices/ 包含了每个系统中出现的设备 指向 设备目录/dev 的动态链接。
that point to the device's directory under root/.
drivers/ contains a directory for each device driver that is loaded
drivers/ 包含了一个每个已为特定总线上的设备而挂载的驱动程序的目录
for devices on that particular bus (this assumes that drivers do not
(这里假定驱动没有多个总线类型)。
span multiple bus types).
fs/ contains a directory for some filesystems. Currently each
fs/ 包含了一个为文件系统设立的目录。 现在每个想要导出属性的 filesystem wanting to export attributes must create its own hierarchy
文件系统必须在 fs/ 下创建自己的层次结构
below fs/ (see ./fuse.txt for an example).
(可参见./fuse.txt 作为参考)。
More information can driver-model specific features can be found in
更多有关driver-model 的特性信息可以在Documentation/driver-model/.
Documentation/driver-model/中找到。
TODO: Finish this section.
Current Interfaces
~~~~~~~~~~~~~~~~~~
当前接口
The following interface layers currently exist in sysfs:
以下的接口层普遍出现在sysfs中:
- devices (include/linux/device.h)
- 设备
----------------------------------
Structure:
结构体:
struct device_attribute {
struct attribute attr;
ssize_t (*show)(struct device * dev, char * buf);
ssize_t (*store)(struct device * dev, const char * buf);
};
Declaring:
声明:
DEVICE_ATTR(_name, _str, _mode, _show, _store);
Creation/Removal:
增/删属性:
int device_create_file(struct device *device, struct device_attribute * attr);
void device_remove_file(struct device * dev, struct device_attribute * attr);
- bus drivers (include/linux/device.h)
- 总线驱动程序
--------------------------------------
Structure:
结构体:
struct bus_attribute {
struct attribute attr;
ssize_t (*show)(struct bus_type *, char * buf);
ssize_t (*store)(struct bus_type *, const char * buf);
};
Declaring:声明:
BUS_ATTR(_name, _mode, _show, _store)
Creation/Removal:
增/删属性:
int bus_create_file(struct bus_type *, struct bus_attribute *);
void bus_remove_file(struct bus_type *, struct bus_attribute *);
- device drivers (include/linux/device.h)
- 设备驱动程序
-----------------------------------------
Structure:
结构体:
struct driver_attribute {
struct attribute attr;
ssize_t (*show)(struct device_driver *, char * buf);
ssize_t (*store)(struct device_driver *, const char * buf);
};
Declaring:
声明:
DRIVER_ATTR(_name, _mode, _show, _store)
Creation/Removal:
增/删属性
int driver_create_file(struct device_driver *, struct driver_attribute *);
void driver_remove_file(struct device_driver *, struct driver_attribute *);
sysfs - 用于导出内核对象(kobject)的文件系统的更多相关文章
- 内核对象kobject和sysfs(3)——kobj分析
内核对象kobject和sysfs(3)--kobj分析 在分析kobj之前,先总结下kobj的功能: 实现结构的动态管理: 实现内核对象到sysfs的映射: 实现自定义属性的管理. 关注一下kobj ...
- 内核对象kobject和sysfs(1)——概述
内核对象kobject和sysfs(1)--概述 问题: 在走读驱动代码时,经常看见kref,kobj,sysfs这些结构,这些结构到底有什么作用?如何理解并使用这些结构呢?这将在接下来的这一系列文章 ...
- 内核对象kobject和sysfs(4)——kset分析
内核对象kobject和sysfs(4)--kset分析 从狭义上来说,kset就是kobj的一个再封装而已.在封装之后,提供了针对kset之下所有kobj统一管理的一些方法. 我们还是从结构说起: ...
- 内核对象kobject和sysfs(2)——kref分析
内核对象kobject和sysfs(2)--kref分析 在介绍ref之前,先贴上kref的结构: struct kref { atomic_t refcount; }; 可以看到,kref只是包含一 ...
- windows核心编程---第三章 内核对象及句柄本质
本章讨论的是相对抽象的概念,不涉及任何具体的内核对象的细节而是讨论所有内核对象的共有特性. 首先让我们来了解一下什么是内核对象.内核对象通过API来创建,每个内核对象是一个数据结构,它对应一块内存 ...
- (转)WINDOWS内核对象
WINDOWS内核对象 原文地址:http://blog.csdn.net/misterliwei/article/details/976988 支持原创 一.前言 Windows中有很多像进程对象 ...
- Windows核心编程&内核对象
1. 一个进程在初始化时,系统将会他分配一个空的句柄表,这个句柄表仅供内核对象使用,不供用户对象和GDI对象使用.进程在首次 初始化时,该句柄表为空.句柄表是一个由数据结构组成的数组,包含一个内核对象 ...
- Windows Internals 笔记——内核对象
1.每个内核对象都只是一个内存块,它由操作系统内核分配,并只能由操作系统内核访问.这个内存块是一个数据结构,其成员维护着与对象相关的信息. 2.调用一个会创建内核对象的函数后,函数会返回一个句柄,它标 ...
- Windows进程的内核对象句柄表
当一个进程被初始化时,系统要为它分配一个句柄表.该句柄表只用于内核对象 ,不用于用户对象或GDI对象. 创建内核对象 当进程初次被初始化时,它的句柄表是空的.然后,当进程中的线程调用创建内核对象的函数 ...
随机推荐
- hdu 1392 Surround the Trees
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1392 题意:给出一些点的坐标,求最小的凸多边形把所有点包围时此多边形的周长. 解法:凸包ConvexH ...
- BZOJ2879 [Noi2012]美食节
AC通道:http://www.lydsy.com/JudgeOnline/problem.php?id=2879 这题codevs上也有,不过数据不同:http://codevs.cn/proble ...
- 日志:slf4j+log4j+maven配置
代码的日志输出,前前后后折腾了我好几次. 本着会用,快速配置的原则,还是将配置过程记录下来,以便复用. 参考:http://blog.csdn.net/anialy/article/details/8 ...
- asp.net word ecxel类型文件在线预览
asp.net word ecxel类型文件在线预览 首先得引用COM: Microsoft Excel 10 Object Library Microsoft Word 10 Object Libr ...
- UglifyJS--javascript代码压缩使用指南{转}
在线测试地址 http://lisperator.net/uglifyjs/下面都是基于linux系统的安装使用.UglifyJS是遵循了CommonJS规范写成的,可以在支持CommonJS模块系统 ...
- leetcode majority number
给定一组数,有一个数在这组数里的出现次数超过n/2次. 求出这是哪个数 https://leetcode.com/problems/majority-element/ 一开始考虑的方是将所有数转化为二 ...
- Form的用法
提交页面: <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w ...
- [STL]deque和stack、queue
怎么说呢,deque是一种双向开口的连续线性空间,至少逻辑上看上去是这样.然而事实上却没有那么简单,准确来说deque其实是一种分段连续空间,因此其实现以及各种操作比vector复杂的多. 一.deq ...
- 2014多校第十场1002 || HDU 4972 A simple dynamic programming problem
题目链接 题意 : 每次无论哪个队投进一个篮球,就记下现在两队比分的差值,问你最后的结果有多少种情况. 思路 : 该题实在是不好理解,最后的结果有多少种情况就是说不管中间过程怎么来的,只要最后结果不一 ...
- 【hdu3579-Hello Kiki】拓展欧几里得-同余方程组
http://acm.hdu.edu.cn/showproblem.php?pid=3579 题解:同余方程组的裸题.注意输出是最小的正整数,不包括0. #include<cstdio> ...