Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 10: Filesystems without Persistent Storage


instead ofprintk,seq_printfis used to format the information. In fact, the kernel provides some auxil-
iary functions that must be used for this purpose. All take a pointer to theseq_fileinstance in question
as first parameter:

❑ seq_printfworks likeprintkand can be used to format arbitrary C strings.
❑ seq_putcandseq_puts, respectively, write out a single character and a string without any
formatting.
❑ seq_esctakes two strings. All characters in the second string that are found in the first string are
replaced by their value in octal.

The special functionsec_pathallows for constructing the filename associated with a given instance of
struct dentry. It is used by filesystem- or namespace-specific code.

Connectionwith the VirtualFilesystem


Up to now, I have presented everything that is required from a sequential file user. The rest, that is,
connecting the operations with the virtual filesystem, is left to the kernel. To establish the connection,
it is necessary to use theseq_readasreadmethod forfile_operationsas shown above in the case of
debugfs_kprobes_operations. The method bridges VFS and sequential files.

First of all, the function needs to obtain theseq_fileinstance from the VFS layer’sstruct file.Recall
thatseq_openedhas established a connection viaprivate_data.

If some data are waiting to be written out — as indicated by a positivecountelement ofstruct
seq_file— , they are copied to userland withcopy_to_user. Additionally, updating the various status
elements ofseq_fileis required.

In the next step, new data are generated. After callingstart, the kernel callsshowandnextone after
another until the available buffer is filled. Finally,stopis employed, and the generated data are copied
to userspace usingcopy_to_user.

10.2.2 Writing Filesystems with Libfs


Libfs is a library that provides several very generic standard routines that can be used to create small
filesystems that serve one specific purpose. The routines are well suited for in-memory files without a
backing store. Obviously the code cannot provide means to interact with specific on-disk formats; this
needs to be handled properly by full filesystem implementations. The library code is contained in a single
file,fs/libfs.c.

The prototypes are defined in<fs.h>;thereisno<libfs.h>! Routines provided by
libfs are generally prefixed bysimple_. Recall from Chapter 8 that the kernel also
provides several generic filesystem routines that are prefixed bygeneric_.Incon-
trast to libfs routines, these can also be used for full-blown filesystems.

The file and directory hierarchy of virtual filesystems that use libfs is generated and traversed using
the dentry tree. This implies that during the lifetime of the filesystem, all dentries must be pinned into
memory. They must not go away unless they are explicitly removed viaunlinkorrmdir. However, this
is simple to achieve: The code only needs to ensure that all dentries always have a positive use count.
Free download pdf