Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 10: Filesystems without Persistent Storage


for operations on files are collected in an instance ofstruct file_operations. For sysfs, the following
selection is available:

fs/sysfs/file.c
const struct file_operations sysfs_file_operations = {
.read = sysfs_read_file,
.write = sysfs_write_file,
.llseek = generic_file_llseek,
.open = sysfs_open_file,
.release = sysfs_release,
.poll = sysfs_poll,
};

In the following, not only are the functions responsible for reading and writing data
(sysfs_{read,write}_file) described, but also the method for opening files (sysfs_open_file)since
the connection between sysfs internals and the virtual filesystem is set up there.

A rather small number of directory inode operations need to be specifically provided by sysfs:

fs/sysfs/dir.c
struct inode_operations sysfs_dir_inode_operations = {
.lookup = sysfs_lookup,
.setattr = sysfs_setattr,
};

Most operations can be handled by standard VFS operations; only directory lookup and attribute mani-
pulation need to be taken care of explicitly. These methods are discussed in the following sections.

The picture is even simpler for inode operations for regular files; only attribute manipulation needs to be
specifically taken care of:

fs/sysfs/inode.c
static struct inode_operations sysfs_inode_operations ={
.setattr = sysfs_setattr,
};

Opening Files


Opening a file is a rather boring operation for a regular filesystem. In the case of sysfs, it becomes more
interesting because the sysfs internal data needs to be connected with the user-visible representation in
the filesystem.

Receiving Data


In order to facilitate the exchange of data between userland and the sysfs implementation, some buffer
space needs to be available. It is provided by the following slightly simplified data structure:

fs/sysfs/file.c
struct sysfs_buffer {
size_t count;
loff_t pos;
char * page;
Free download pdf