Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 10: Filesystems without Persistent Storage


sysfs_put_active_two(attr_sd);
return 0;
}

Finally,sysfs_get_open_direntconnects the freshly allocated buffer with the sysfs data structures
viasysfs_open_direntas shown in Figure 10-12. Note that since no further access to thekobjects
associated with the sysfs entry is required anymore, the active references can (and need!) be dropped
usingsysfs_put_active_two.

Reading andWriting File Contents


Recall thatsysfs_file_operationsspecifies the methods used by the VFS to access the content of files
in sysfs. After having introduced all necessary data structures for reading and writing data, it is now time
to discuss these operations.

Reading


Reading data is delegated tosysfs_read_file; the associated code flow diagram can be found in
Figure 10-14.

sys_read_file

Buffer refill needed? fill_read_buffer

simple_read_from_buffer

Figure 10-14: Code flow diagram forsysfs_read_file.

The implementation is comparatively simple: If the data buffer is not yet filled in because it is accessed for
the first time or has been modified by a write operation (both indicated bybuffer->needs_read_fill),
fill_read_bufferneeds to be called to fill the buffer first. This function is responsible for two things:


  1. Allocate a (zero-filled) page frame to hold the data.

  2. Call theshowmethod of thestruct sysfs_opsinstance to provide the buffer contents, that
    is, fill in data to the page frame allocated above.


Once the buffer is filled with data, the remaining work is delegated tosimple_read_from_buffer.As
you might have guessed from the name, the task is simple and requires only some bounds checking and
a memory copy operation from kernel to userspace.

Writing


To allow the reverse process, namely, writing data from user to kernel space,sysfs_write_fileis pro-
vided. Like for the read companion, the implementation is quite simple as the code flow diagram in
Figure 10-15 shows.

First,fill_write_bufferallocates a page frame into which the data given from userspace are copied.
This setsbuffer->needs_refillbecause the content of the buffer needs to be refreshed if a read request
takes place after the write. The remaining work is delegated toflush_write_buffer;itsmainjobisto
call thestoremethod provided by thesysfs_opsinstance specific to the file.
Free download pdf