Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 10: Filesystems without Persistent Storage


ops = kobj->kset->ktype->sysfs_ops;
else if (kobj->ktype)
ops = kobj->ktype->sysfs_ops;
else
ops = &subsys_sysfs_ops;
...

Since all members of kernel subsystems are collected in akset, this allows for connecting attributes at
a subsystem-specific level because the same access functions are used for all elements. If thekobject
under consideration is not contained in akset, then it is still possible that it has aktypefrom which the
sysfs_opscan be taken. It is up to the subsystem how to implement thesysfs_ops,butthemethods
used are quite similar, as shown in Section 10.3.5.


If something is supposed to be written into the file, it is not sufficient to just check if the access mode
bits allow this. Additionally, the entry is required to provide astoreoperation in thesysfs_ops.Itdoes
not make sense to grant read access if there is no function that can actually present data to userspace. A
similar condition holds for read access:


fs/sysfs/file.c
/* File needs write support.
* The inode’s perms must say it’s ok,
* and we must have a store method.
*/
if (file->f_mode & FMODE_WRITE) {
if (!(inode->i_mode & S_IWUGO) || !ops->store)
goto err_out;
}

/* File needs read support.
* The inode’s perms must say it’s ok, and we there
* must be a show method for it.
*/
if (file->f_mode & FMODE_READ) {
if (!(inode->i_mode & S_IRUGO) || !ops->show)
goto err_out;
}
...

After the kernel has chosen to allow the access, an instance ofsysfs_bufferis allocated, filled in with
the appropriate elements, and connected to the file viafile->private_dataas shown below:


fs/sysfs/file.c
buffer = kzalloc(sizeof(struct sysfs_buffer), GFP_KERNEL);
...
mutex_init(&buffer->mutex);
buffer->needs_read_fill = 1;
buffer->ops = ops;
file->private_data = buffer;

/* make sure we have open dirent struct */
error = sysfs_get_open_dirent(attr_sd, buffer);
...
/* open succeeded, put active references */
Free download pdf