Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 10: Filesystems without Persistent Storage


sysfs_elem_attrandsysfs_bin_attrcontain pointers to data structures that represent
attributes, and are discussed in the following section.sysfs_elem_symlinkimplements a
symbolic link. All it needs to do is provide a pointer to the targetsysfs_direntinstance.
Directories are implemented with the aid ofsysfs_elem_dir.childrenis the head of a singly
linked list connecting all children vias_sibling. Note that the elements on the sibling list are
sorted bys_inoin decreasing order. The relationship is illustrated in Figure 10-9.

Like any other filesystem, sysfs entries are also represented by instances ofstruct dentry. The con-
nection between both layers is given bydentry->d_fsdata, which points to thesysfs_direntinstance
associated with the dentry element.

s_dir.children s_sbiling

s ino = 1000 s ino = 953

s_sbiling

d_fsdata

struct dentry

sysfs_dirent

Figure 10-9: Sysfs directory hierarchy based onstruct sysfs_dirent.

Reference counting forstruct sysfs_direntis unconventional because two reference counters are pro-
vided:s_countands_active. The first one is a standard reference counter that needs to be incremented
when thesysfs_direntinstance under consideration is required by some part of the kernel and decre-
mented when it is not required anymore. A problem arises, though, because whenever a sysfs node is
opened, the associatedkobjectis also referenced. Userland applications could thus prevent the ker-
nel from deletingkobjectinstances by simply keeping a sysfs file open. To circumvent this, the kernel
requires that anactivereference on asysfs_direntryis held whenever the associated internal objects
(available viasysfs_elem_*) are accessed. Unsurprisingly, the active reference counter is implemented
withs_active.

When a sysfs file is supposed to be deleted, access to the internal objects associated with it can be
deactivated by setting the active reference counter to a negative value — the auxiliary function
sysfs_dectivateis provided for this. Once the value is negative, operations on the associatedkobject
cannot be performed anymore. When all users of thekobjecthave disappeared, it can safely be deleted
by the kernel. The sysfs file and thus thesysfs_direntinstance, however, can still exist — even if they
do not make much sense anymore!

Active references can be obtained bysysfs_get_activeorsysfs_get_active_two(for a given
sysfs_direntryinstance as well as its parent). They must immediately be released with
Free download pdf