Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 10: Filesystems without Persistent Storage


size_t size;
size_t from;
size_t count;
loff_t index;
...
const struct seq_operations *op;
...
};

bufpoints to a memory buffer that allows for constructing data that go out to userland.countspecifies
the number of bytes remaining to be written to userland. The start position of the copy operation is
denoted byfrom,andsizegives the total number of bytes in the buffer.indexis another index into the
buffer. It marks the start position for the next new record that is written into the buffer by the kernel. Note
thatindexandfromcan evolve differently since writing data into the buffer is different from copying
these data to userspace.

The most important element from a filesystem implementor’s point of view is the pointeropto an
instance ofseq_operations. This connects the generic sequential file implementation with routines
providing file-specific contents. Four methods are required by the kernel and need to be implemented by
the file provider:

<seq_file.h>
struct seq_operations {
void * (*start) (struct seq_file *m, loff_t *pos);
void (*stop) (struct seq_file *m, void *v);
void * (*next) (struct seq_file *m, void *v, loff_t *pos);
int (*show) (struct seq_file *m, void *v);
};

Thefirstargumenttothefunctionsisalwaystheseq_fileinstance in question. The start method is
called whenever an operation on a sequential file is started. The position argumentposis a cursor in the
file. The interpretation is left to the implementation. It could be taken as a byte offset, but can also be
interpreted as an array index. The kprobes exampleimplements all these routines as shown above, so
they are discussed now.

Let us first, however, briefly describe which type of information is passed to userland — we need to know
what goes out before we can discuss how it goes out. The kprobes mechanism allows for attaching probes
to certain points in the kernel. All registered probes are hashed on the arraykprobe_table,andthesize
of the array is statically defined toKPROBE_TABLE_SIZE. The file cursor for sequential files is interpreted
as an index into the array, and the debug file is supposed to show information about all registered probes
that must be constructed from the contents of the hash table.

Thestartmethod is simple: It just needs to check if the current cursor is beyond the array bounds.

kernel/kprobes.c
static void __kprobes *kprobe_seq_start(struct seq_file *f, loff_t *pos)
{
return (*pos < KPROBE_TABLE_SIZE)? pos : NULL;
}

This is simple, but closing a sequential file is even simpler: In almost all cases, nothing needs to be done!
Free download pdf