Sysfs 是 Linux 2.6所提供的一种虚拟文件系统。这个文件系统不仅可以把设备(devices)和驱动程序(drivers) 的信息从内核输出到用户空间,也可以用来对设备和驱动程序做设置。
procfs太过于混乱,不利于查看设备驱动信息。sysfs 的目的是把一些原本在 procfs中的,关于设备的部份,独立出来,以‘设备层次结构架构’(device tree)的形式呈现。
The sysfs directoryarrangement exposes the relationship of kernel
data structures.
The top level sysfsdirectory looks like:
block/
bus/
class/
devices/
firmware/
net/
fs/
devices/ contains afilesystem representation of the device tree. It maps
directly to theinternal kernel device tree, which is a hierarchy of
struct device.
bus/ contains flatdirectory layout of the various bus types in the
kernel. Each bus'sdirectory contains two subdirectories:
devices/
drivers/
devices/ containssymlinks for each device discovered in the system
that point to thedevice's directory under root/.
drivers/ contains adirectory for each device driver that is loaded
for devices on thatparticular bus (this assumes that drivers do not
span multiple bustypes).
fs/ contains adirectory for some filesystems. Currently each
filesystem wantingto export attributes must create its own hierarchy
below fs/ (see./fuse.txt for an example).
More information candriver-model specific features can be found in
Documentation/driver-model/.
TODO: Finish thissection.
摘自Doc/filesystem/sysfs.txt
关于sysfs,首先要搞清楚,kset,kobject
-kobjects a simple object.
-kset aset of objects of a certain type.
-ktype aset of helpers for objects of a common type.
-subsystem a controlling objectfor a number of ksets.
struct kobject {
char name[KOBJ_NAME_LEN]; //名字
atomic_t refcount; //引用数
structlist_head entry; //kobject之间的双向链表,用于跟所属kset形成环形链表
structkobject *parent; //它的父kobject
structkset *kset; //所属的kset
structkobj_type * ktype; //负责对该kobject类型进行跟踪的struct
structdentry *dentry;//目录
};
struct kset {
structsubsystem * subsys;
structkobj_type * ktype; //指向该kset对象类型的指针
structlist_head list; //与它所含的kobject形成环形链表
spinlock_t list_lock;
structkobject kobj; //它自己也是个object
structkset_uevent_ops * uevent_ops; //ops
};
上面两张图,谷歌而得的,很赞