Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 10: Filesystems without Persistent Storage


When a sysfs file related to generic hard disk attributes is accessed, the kernel uses the methods
disk_attr_showanddisk_attr_storeto read and modify the attribute values. Thedisk_attr_show
function is called whenever the value of an attribute of this type needs to be read from the kernel; the
code acts as the glue between sysfs and the genhd implementation:

block/genhd.c
static ssize_t disk_attr_show(struct kobject *kobj, struct attribute *attr,
char *page)
{
struct gendisk *disk = to_disk(kobj);
struct disk_attribute *disk_attr =
container_of(attr,struct disk_attribute,attr);
ssize_t ret = -EIO;

if (disk_attr->show)
ret = disk_attr->show(disk,page);
return ret;
}

The attribute connected to the sysfs file can be used to infer the containingdisk_attributeinstance by
using thecontainer_of-mechanism; after the kernel has made sure that the attribute possesses a show
method, it is called to transfer data from the kernel to userspace and thus from the internal data structures
to the sysfs file.

Similar methods are implemented by many other subsystems, but since their code is basically identical
to the example shown above, it is unnecessary to consider them in greater detail here. Instead, I will
cover the steps leading to a call of the sysfs-specificshowandstoremethods; the connection between
subsystem and sysfs is left to the subsystem-specific code.

10.3.3 Mounting the Filesystem


As usual, let’s start the discussion of the implementation by considering how the filesystem is mounted.
The system call ends up in delegating the work to fill a superblock tosysfs_fill_super; the associated
code flow diagram can be found in Figure 10-10.

sysfs_fill_super

sysfs_get_inode

sysfs_init_inode

d_alloc_root

root->d_fsdata = &sysfs_root

Figure 10-10: Code flow diagram for
sysfs_fill_super.

There is not too much to do forsysfs_fill_super: Some uninteresting initialization work needs to
be performed first.sysfs_get_inodeis then used to create a new instance ofstruct inodeas the
Free download pdf