Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 10: Filesystems without Persistent Storage


Writing SequentialFile Handlers


Basically, an instance ofstruct file_operationsthat provides pointers to someseq_routines must be
implemented to benefit from the sequential file standard implementation. The kprobes subsystem does
this as follows:

kernel/kprobes.c
static struct file_operations debugfs_kprobes_operations = {
.open = kprobes_open,
.read = seq_read,
.llseek = seq_lseek,
.release = seq_release,
};

This instance offile_operationscan be associated with a file by the methods discussed in Chapter 8. In
the case of kprobes, the file will be created in the debugging filesystem; see Section 10.2.3.

The only method that needs to be implemented isopen. Not much effort is required for the function,
though: A simple one-liner connects the file with the sequential file interface:

kernel/kprobes.c
static struct seq_operations kprobes_seq_ops = {
.start = kprobe_seq_start,
.next = kprobe_seq_next,
.stop = kprobe_seq_stop,
.show = show_kprobe_addr
};

static int __kprobes kprobes_open(struct inode *inode, struct file *filp)
{
return seq_open(filp, &kprobes_seq_ops);
}

struct file struct seq_file struct seq_operations

private op

start
stop
next
show

Figure 10-8: Data structures for sequential files.

seq_opensets up the data structures required by the sequential file mechanism. The result is shown in
Figure 10-8. Recall from Chapter 8 that theprivate_dataelement ofstruct filecan point to arbitrary
data that are private to the file and not touched by the generic VFS functions. In this case,seq_openuses
the pointer to establish a connection with an instance ofstruct seq_filethat contains status information
about the sequential file:

<seq_file.h>
struct seq_file {
char *buf;
Free download pdf