322 • The sysfs Filesystem
attributes from sysfs for each network device.
In this case, the module field should point to
the module that contains the network statistic
attributes.
The owner field is used for reference count-
ing when the attribute file is accessed. The file
operations for attributes that the VFS calls are
set by sysfs with internal functions. This allows
sysfs to trap each access call and perform nec-
essary actions, and it allows the actual methods
that read and write attribute data to be greatly
simplified.
When an attribute file is opened, sysfs incre-
ments the reference count of both the kobject
represented by the directory where the attribute
resides, and the module which contains the at-
tribute code. The former operation guarantees
that the kobject will not be freed while the at-
tribute is being accessed. The latter guarantees
that the code which is being executed will not
be unloaded from the kernel and freed while the
attribute is being accessed.
7.3 Wrappable Objects
One will notice that struct attribute
does not actually contain the methods to read
or write the attribute. sysfs does not specify the
format or parameters of these functions. This
was an explicit design decision to help ensure
type safety in these functions, and to aid in sim-
plifying the downstream methods.
Subsystems that use sysfs attributes create
a new data type that encapsulates struct
attribute, like in Table 10. By defining a
wrapping data type and functions, downstream
code is protected from the low-level details of
sysfs and kobject semantics.
When an attribute is read or written, sysfs ac-
cesses a special data structure, through the kob-
ject, called a kset. This contains the base opera-
tions for reading and writing attributes for kob-
jects of a particular type. These functions trans-
late the kobject and attribute into higher level
objects, which are then passed to the show and
store methods described in Table 10. Again,
this helps ensure type safety, because it guar-
antees that the downstream function receives a
higher-level object that it use directly, without
having to translate it.
Many programmers are inclined to cast be-
tween object types, which can lead to hard-to-
find bugs if the position of the fields in a struc-
ture changes. By using helper functions within
the kernel that perform an offset-based pointer
subtraction to translate between object types,
type safety can be guaranteed, regardless if the
field locations may change. By centralizing the
translation of objects in this manner, the code
can be easier to audit in the event of change.
7.4 Reading and Writing Attributes
sysfs attempts to make reading and writing at-
tributes as simple as possible. When an at-
tribute is opened, a PAGE_SIZE buffer is al-
located for transferring the data between the
kernel and userspace. When an attribute
is read, this buffer is passed to a down-
stream function (e.g., struct device_
attribute::show() which is responsible
for filling in the data and formatting it appro-
priately. This data is then copied to userspace.
When a value is written to a sysfs attribute file,
the data is first copied to the kernel buffer, then
it is passed to the downstream method, along
with the size of the buffer in bytes. This method
is responsible for parsing the data.
It is assumed that the data written to the buffer
is in ASCII format. It is also implied that the
size of the data written is less than one page in