Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 10: Filesystems without Persistent Storage


It is the responsibility of the code that declaresa new attribute type to provide a suitable set ofshowand
storeoperations.

The situation is different for binary attributes: Here, the methods used to read and modify the data are
usually different for each attribute. This is reflected in the data structure, where methods for reading,
writing, and memory mapping are specified explicitly:

<sysfs.h>
struct bin_attribute {
struct attribute attr;
size_t size;
void *private;
ssize_t (*read)(struct kobject *, struct bin_attribute *,
char *, loff_t, size_t);
ssize_t (*write)(struct kobject *, struct bin_attribute *,
char *, loff_t, size_t);
int (*mmap)(struct kobject *, struct bin_attribute *attr,
struct vm_area_struct *vma);
};

sizedenotes the size of the binary data associated with the attribute, andprivateis (usually) used to
point to the place where the data are actually stored.

Declaring New Attributes


Many possibilities for declaring subsystem-specific attributes are spread around the kernel, but since
they all share a basic structure with regard to their implementation, it is sufficient to consider one imple-
mentation as an example for the underlying mechanism. Consider, for instance, how the generic hard
disk code defines a structure that unites an attribute and the associated methods to read and modify the
attribute:

<genhd.h>
struct disk_attribute {
struct attribute attr;
ssize_t (*show)(struct gendisk *, char *);
ssize_t (*store)(struct gendisk *, const char *, size_t);
};

Theattrmember is nothing other than an attribute as introduced before; this can be fed to sysfs when-
ever an instance ofattributeis required. But note that theshowandstorefunction pointers have a
different prototype from that required for sysfs!

How do the subsystem-specific attribute functions get called by the sysfs layer? The connection is made
by the following struct:

block/genhd.c
static struct sysfs_ops disk_sysfs_ops = {
.show = &disk_attr_show,
.store = &disk_attr_store,
};

Theshowandstoremethods ofsysfs_opsare called when a process wants to read from (or write to) a
sysfs file as will be shown below in more detail.
Free download pdf