xref: /linux/Documentation/translations/zh_CN/filesystems/sysfs.txt (revision 3503d56cc7233ced602e38a4c13caa64f00ab2aa)
1Chinese translated version of Documentation/filesystems/sysfs.rst
2
3If you have any comment or update to the content, please contact the
4original document maintainer directly.  However, if you have a problem
5communicating in English you can also ask the Chinese maintainer for
6help.  Contact the Chinese maintainer if this translation is outdated
7or if there is a problem with the translation.
8
9Maintainer: Patrick Mochel	<mochel@osdl.org>
10		Mike Murphy <mamurph@cs.clemson.edu>
11Chinese maintainer: Fu Wei <tekkamanninja@gmail.com>
12---------------------------------------------------------------------
13Documentation/filesystems/sysfs.rst 的中文翻译
14
15如果想评论或更新本文的内容,请直接联系原文档的维护者。如果你使用英文
16交流有困难的话,也可以向中文版维护者求助。如果本翻译更新不及时或者翻
17译存在问题,请联系中文版维护者。
18英文版维护者: Patrick Mochel	<mochel@osdl.org>
19		Mike Murphy <mamurph@cs.clemson.edu>
20中文版维护者: 傅炜 Fu Wei <tekkamanninja@gmail.com>
21中文版翻译者: 傅炜 Fu Wei <tekkamanninja@gmail.com>
22中文版校译者: 傅炜 Fu Wei <tekkamanninja@gmail.com>
23
24
25以下为正文
26---------------------------------------------------------------------
27sysfs - 用于导出内核对象(kobject)的文件系统
28
29Patrick Mochel	<mochel@osdl.org>
30Mike Murphy <mamurph@cs.clemson.edu>
31
32修订:    16 August 2011
33原始版本:   10 January 2003
34
35
36sysfs 简介:
37~~~~~~~~~~
38
39sysfs 是一个最初基于 ramfs 且位于内存的文件系统。它提供导出内核
40数据结构及其属性,以及它们之间的关联到用户空间的方法。
41
42sysfs 始终与 kobject 的底层结构紧密相关。请阅读
43Documentation/core-api/kobject.rst 文档以获得更多关于 kobject 接口的
44信息。
45
46
47使用 sysfs
48~~~~~~~~~~~
49
50只要内核配置中定义了 CONFIG_SYSFS ,sysfs 总是被编译进内核。你可
51通过以下命令挂载它:
52
53    mount -t sysfs sysfs /sys
54
55
56创建目录
57~~~~~~~~
58
59任何 kobject 在系统中注册,就会有一个目录在 sysfs 中被创建。这个
60目录是作为该 kobject 的父对象所在目录的子目录创建的,以准确地传递
61内核的对象层次到用户空间。sysfs 中的顶层目录代表着内核对象层次的
62共同祖先;例如:某些对象属于某个子系统。
63
64Sysfs 在与其目录关联的 kernfs_node 对象中内部保存一个指向实现
65目录的 kobject 的指针。以前,这个 kobject 指针被 sysfs 直接用于
66kobject 文件打开和关闭的引用计数。而现在的 sysfs 实现中,kobject
67引用计数只能通过 sysfs_schedule_callback() 函数直接修改。
68
69
70属性
71~~~~
72
73kobject 的属性可在文件系统中以普通文件的形式导出。Sysfs 为属性定义
74了面向文件 I/O 操作的方法,以提供对内核属性的读写。
75
76
77属性应为 ASCII 码文本文件。以一个文件只存储一个属性值为宜。但一个
78文件只包含一个属性值可能影响效率,所以一个包含相同数据类型的属性值
79数组也被广泛地接受。
80
81混合类型、表达多行数据以及一些怪异的数据格式会遭到强烈反对。这样做是
82很丢脸的,而且其代码会在未通知作者的情况下被重写。
83
84
85一个简单的属性结构定义如下:
86
87struct attribute {
88        char                    * name;
89        struct module		*owner;
90        umode_t                 mode;
91};
92
93
94int sysfs_create_file(struct kobject * kobj, const struct attribute * attr);
95void sysfs_remove_file(struct kobject * kobj, const struct attribute * attr);
96
97
98一个单独的属性结构并不包含读写其属性值的方法。子系统最好为增删特定
99对象类型的属性定义自己的属性结构体和封装函数。
100
101例如:驱动程序模型定义的 device_attribute 结构体如下:
102
103struct device_attribute {
104	struct attribute	attr;
105	ssize_t (*show)(struct device *dev, struct device_attribute *attr,
106			char *buf);
107	ssize_t (*store)(struct device *dev, struct device_attribute *attr,
108			 const char *buf, size_t count);
109};
110
111int device_create_file(struct device *, const struct device_attribute *);
112void device_remove_file(struct device *, const struct device_attribute *);
113
114为了定义设备属性,同时定义了一下辅助宏:
115
116#define DEVICE_ATTR(_name, _mode, _show, _store) \
117struct device_attribute dev_attr_##_name = __ATTR(_name, _mode, _show, _store)
118
119例如:声明
120
121static DEVICE_ATTR(foo, S_IWUSR | S_IRUGO, show_foo, store_foo);
122
123等同于如下代码:
124
125static struct device_attribute dev_attr_foo = {
126       .attr	= {
127		.name = "foo",
128		.mode = S_IWUSR | S_IRUGO,
129		.show = show_foo,
130		.store = store_foo,
131	},
132};
133
134
135子系统特有的回调函数
136~~~~~~~~~~~~~~~~~~~
137
138当一个子系统定义一个新的属性类型时,必须实现一系列的 sysfs 操作,
139以帮助读写调用实现属性所有者的显示和储存方法。
140
141struct sysfs_ops {
142        ssize_t (*show)(struct kobject *, struct attribute *, char *);
143        ssize_t (*store)(struct kobject *, struct attribute *, const char *, size_t);
144};
145
146[子系统应已经定义了一个 struct kobj_type 结构体作为这个类型的
147描述符,并在此保存 sysfs_ops 的指针。更多的信息参见 kobject 的
148文档]
149
150sysfs 会为这个类型调用适当的方法。当一个文件被读写时,这个方法会
151将一般的kobject 和 attribute 结构体指针转换为适当的指针类型后
152调用相关联的函数。
153
154
155示例:
156
157#define to_dev(obj) container_of(obj, struct device, kobj)
158#define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
159
160static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr,
161                             char *buf)
162{
163        struct device_attribute *dev_attr = to_dev_attr(attr);
164        struct device *dev = to_dev(kobj);
165        ssize_t ret = -EIO;
166
167        if (dev_attr->show)
168                ret = dev_attr->show(dev, dev_attr, buf);
169        if (ret >= (ssize_t)PAGE_SIZE) {
170                printk("dev_attr_show: %pS returned bad count\n",
171                                dev_attr->show);
172        }
173        return ret;
174}
175
176
177
178读写属性数据
179~~~~~~~~~~~~
180
181在声明属性时,必须指定 show() 或 store() 方法,以实现属性的
182读或写。这些方法的类型应该和以下的设备属性定义一样简单。
183
184ssize_t (*show)(struct device *dev, struct device_attribute *attr, char *buf);
185ssize_t (*store)(struct device *dev, struct device_attribute *attr,
186                 const char *buf, size_t count);
187
188也就是说,他们应只以一个处理对象、一个属性和一个缓冲指针作为参数。
189
190sysfs 会分配一个大小为 (PAGE_SIZE) 的缓冲区并传递给这个方法。
191Sysfs 将会为每次读写操作调用一次这个方法。这使得这些方法在执行时
192会出现以下的行为:
193
194- 在读方面(read(2)),show() 方法应该填充整个缓冲区。回想属性
195  应只导出了一个属性值或是一个同类型属性值的数组,所以这个代价将
196  不会不太高。
197
198  这使得用户空间可以局部地读和任意的向前搜索整个文件。如果用户空间
199  向后搜索到零或使用‘0’偏移执行一个pread(2)操作,show()方法将
200  再次被调用,以重新填充缓存。
201
202- 在写方面(write(2)),sysfs 希望在第一次写操作时得到整个缓冲区。
203  之后 Sysfs 传递整个缓冲区给 store() 方法。
204
205  当要写 sysfs 文件时,用户空间进程应首先读取整个文件,修该想要
206  改变的值,然后回写整个缓冲区。
207
208  在读写属性值时,属性方法的执行应操作相同的缓冲区。
209
210注记:
211
212- 写操作导致的 show() 方法重载,会忽略当前文件位置。
213
214- 缓冲区应总是 PAGE_SIZE 大小。对于i386,这个值为4096。
215
216- show() 方法应该返回写入缓冲区的字节数,也就是 snprintf()的
217  返回值。
218
219- show() 应始终使用 snprintf()。
220
221- store() 应返回缓冲区的已用字节数。如果整个缓存都已填满,只需返回
222  count 参数。
223
224- show() 或 store() 可以返回错误值。当得到一个非法值,必须返回一个
225  错误值。
226
227- 一个传递给方法的对象将会通过 sysfs 调用对象内嵌的引用计数固定在
228  内存中。尽管如此,对象代表的物理实体(如设备)可能已不存在。如有必要,
229  应该实现一个检测机制。
230
231一个简单的(未经实验证实的)设备属性实现如下:
232
233static ssize_t show_name(struct device *dev, struct device_attribute *attr,
234                         char *buf)
235{
236	return scnprintf(buf, PAGE_SIZE, "%s\n", dev->name);
237}
238
239static ssize_t store_name(struct device *dev, struct device_attribute *attr,
240                          const char *buf, size_t count)
241{
242        snprintf(dev->name, sizeof(dev->name), "%.*s",
243                 (int)min(count, sizeof(dev->name) - 1), buf);
244	return count;
245}
246
247static DEVICE_ATTR(name, S_IRUGO, show_name, store_name);
248
249
250(注意:真正的实现不允许用户空间设置设备名。)
251
252顶层目录布局
253~~~~~~~~~~~~
254
255sysfs 目录的安排显示了内核数据结构之间的关系。
256
257顶层 sysfs 目录如下:
258
259block/
260bus/
261class/
262dev/
263devices/
264firmware/
265net/
266fs/
267
268devices/ 包含了一个设备树的文件系统表示。他直接映射了内部的内核
269设备树,反映了设备的层次结构。
270
271bus/ 包含了内核中各种总线类型的平面目录布局。每个总线目录包含两个
272子目录:
273
274	devices/
275	drivers/
276
277devices/ 包含了系统中出现的每个设备的符号链接,他们指向 root/ 下的
278设备目录。
279
280drivers/ 包含了每个已为特定总线上的设备而挂载的驱动程序的目录(这里
281假定驱动没有跨越多个总线类型)。
282
283fs/ 包含了一个为文件系统设立的目录。现在每个想要导出属性的文件系统必须
284在 fs/ 下创建自己的层次结构(参见Documentation/filesystems/fuse.rst)。
285
286dev/ 包含两个子目录: char/ 和 block/。在这两个子目录中,有以
287<major>:<minor> 格式命名的符号链接。这些符号链接指向 sysfs 目录
288中相应的设备。/sys/dev 提供一个通过一个 stat(2) 操作结果,查找
289设备 sysfs 接口快捷的方法。
290
291更多有关 driver-model 的特性信息可以在 Documentation/driver-api/driver-model/
292中找到。
293
294
295TODO: 完成这一节。
296
297
298当前接口
299~~~~~~~~
300
301以下的接口层普遍存在于当前的sysfs中:
302
303- 设备 (include/linux/device.h)
304----------------------------------
305结构体:
306
307struct device_attribute {
308	struct attribute	attr;
309	ssize_t (*show)(struct device *dev, struct device_attribute *attr,
310			char *buf);
311	ssize_t (*store)(struct device *dev, struct device_attribute *attr,
312			 const char *buf, size_t count);
313};
314
315声明:
316
317DEVICE_ATTR(_name, _mode, _show, _store);
318
319增/删属性:
320
321int device_create_file(struct device *dev, const struct device_attribute * attr);
322void device_remove_file(struct device *dev, const struct device_attribute * attr);
323
324
325- 总线驱动程序 (include/linux/device.h)
326--------------------------------------
327结构体:
328
329struct bus_attribute {
330        struct attribute        attr;
331        ssize_t (*show)(struct bus_type *, char * buf);
332        ssize_t (*store)(struct bus_type *, const char * buf, size_t count);
333};
334
335声明:
336
337BUS_ATTR(_name, _mode, _show, _store)
338
339增/删属性:
340
341int bus_create_file(struct bus_type *, struct bus_attribute *);
342void bus_remove_file(struct bus_type *, struct bus_attribute *);
343
344
345- 设备驱动程序 (include/linux/device.h)
346-----------------------------------------
347
348结构体:
349
350struct driver_attribute {
351        struct attribute        attr;
352        ssize_t (*show)(struct device_driver *, char * buf);
353        ssize_t (*store)(struct device_driver *, const char * buf,
354                         size_t count);
355};
356
357声明:
358
359DRIVER_ATTR(_name, _mode, _show, _store)
360
361增/删属性:
362
363int driver_create_file(struct device_driver *, const struct driver_attribute *);
364void driver_remove_file(struct device_driver *, const struct driver_attribute *);
365
366
367文档
368~~~~
369
370sysfs 目录结构以及其中包含的属性定义了一个内核与用户空间之间的 ABI。
371对于任何 ABI,其自身的稳定和适当的文档是非常重要的。所有新的 sysfs
372属性必须在 Documentation/ABI 中有文档。详见 Documentation/ABI/README373