Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 10: Filesystems without Persistent Storage


10.1.6 Reading and Writing Information


As noted in Section 10.1.5, the kernel uses the operations stored inproc_file_operationsto read and
write the contents of regularprocentries. The contents of the function pointers in this structure are as
follows:

fs/proc/generic.c
static struct file_operations proc_file_operations = {
.llseek = proc_file_lseek,
.read = proc_file_read,
.write = proc_file_write,
};

The sections below examine the read and write operations implemented by means ofproc_file_read
andproc_file_write.

Implementationofproc_file_read


Data are read from aprocfile in three steps:


  1. A kernel memory page is allocated into which data are generated.

  2. A file-specific function is invoked to fill the kernel memory page with data.

  3. The data are copied from kernel space to userspace.


Obviously, the second step is the most important because the subsystem data and kernel data structures
must be specially prepared. The other two steps are simple routine tasks. Section 10.1.2 noted that the
kernel provides two function pointers toget_infoandread_procin theproc_dir_entrystructure;
these functions are used to read data, and the kernel must select the one that matches.

fs/proc/generic.c
proc_file_read(struct file *file, char __user *buf, size_t nbytes,
loff_t *ppos)
{
...
if (dp->get_info) {
/* Handle old net routines */
n = dp->get_info(page, &start, *ppos, count);
if (n < count)
eof = 1;
} else if (dp->read_proc) {
n = dp->read_proc(page, &start, *ppos,
count, &eof, dp->data);
} else
break;
...
}

pageis a pointer to the memory page allocated to hold the data in the first step.

Since a sample implementation ofread_procis included in Section 10.1.5, it need not be repeated here.
Free download pdf