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