Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 10: Filesystems without Persistent Storage


10.3.5 Populating Sysfs


Since sysfs is an interface to export data from the kernel, only the kernel itself can populate sysfs with file
and directory entries. This can be triggered from places all over the kernel, and, indeed, such operations
are ubiquitous within the whole tree, which renders it impossible to cover all appearances in detail. Thus
only the general methods used to connect sysfs with theinternal data structures of the diverse subsystems
are demonstrated; the methods used for this purpose are quite similar everywhere.

Registering Subsystems
Once more I use the generic hard disk code as an example for a subsystem that uses kobjects that are
represented in sysfs. Observe that the directory/sys/blockis used to represent this subsystem. For
every block device available in the system, a subdirectory contains several attribute files:


root@meitner #ls -l /sys/block
total 0
drwxr-xr-x 4 root root 0 2008-02-09 23:26 loop0
drwxr-xr-x 4 root root 0 2008-02-09 23:26 loop1
drwxr-xr-x 4 root root 0 2008-02-09 23:26 loop2
drwxr-xr-x 4 root root 0 2008-02-09 23:26 loop3
drwxr-xr-x 4 root root 0 2008-02-09 23:26 loop4
drwxr-xr-x 4 root root 0 2008-02-09 23:26 loop5
drwxr-xr-x 4 root root 0 2008-02-09 23:26 loop6
drwxr-xr-x 4 root root 0 2008-02-09 23:26 loop7
drwxr-xr-x 10 root root 0 2008-02-09 23:26 sda
drwxr-xr-x 5 root root 0 2008-02-09 23:26 sdb
drwxr-xr-x 5 root root 0 2008-02-09 23:26 sr0
root@meitner #ls -l /sys/block/hda
total 0
-r--r--r-- 1 root root 4096 2008-02-09 23:26 capability
-r--r--r-- 1 root root 4096 2008-02-09 23:26 dev
lrwxrwxrwx 1 root root 0 2008-02-09 23:26 device -> ../../devices/pci0000:00/
0000:00:1f.2/host0/target0:0:0/0:0:0:0
drwxr-xr-x 2 root root 0 2008-02-09 23:26 holders
drwxr-xr-x 3 root root 0 2008-02-09 23:26 queue
-r--r--r-- 1 root root 4096 2008-02-09 23:26 range
-r--r--r-- 1 root root 4096 2008-02-09 23:26 removable
drwxr-xr-x 3 root root 0 2008-02-09 23:26 sda1
drwxr-xr-x 3 root root 0 2008-02-09 23:26 sda2
drwxr-xr-x 3 root root 0 2008-02-09 23:26 sda5
drwxr-xr-x 3 root root 0 2008-02-09 23:26 sda6
drwxr-xr-x 3 root root 0 2008-02-09 23:26 sda7
-r--r--r-- 1 root root 4096 2008-02-09 23:26 size
drwxr-xr-x 2 root root 0 2008-02-09 23:26 slaves
-r--r--r-- 1 root root 4096 2008-02-09 23:26 stat
lrwxrwxrwx 1 root root 0 2008-02-09 23:26 subsystem -> ../../block
--w------- 1 root root 4096 2008-02-09 23:26 uevent


One of the central elements behind this output is thefollowing data structure, which connects a sysfs-
specificattributestructure with genhd-specificstoreandshowmethods. Note that these methods do
nothave the signature required for theshow/storemethods required by sysfs; these will be provided
later:
<genhd.h>
struct disk_attribute {
struct attribute attr;
Free download pdf