Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 10: Filesystems without Persistent Storage


Implementationofproc_file_write


Writing toprocfiles is also a simple matter — at least from the perspective of the filesystem. The code of
proc_file_writeis very compact and thus is reproduced in full below.

fs/proc/generic.c
static ssize_t
proc_file_write(struct file * file, const char __user *buffer,
size_t count, loff_t *ppos)
{
struct inode *inode = file->f_dentry->d_inode;
struct proc_dir_entry * dp;

dp = PDE(inode);

if (!dp->write_proc)
return -EIO;

return dp->write_proc(file, buffer, count, dp->data);
}

ThePDEfunction needed to obtain the requiredproc_dir_entryinstance from the VFS inode using
the container mechanism is very simple. All it does is executePROC_I(inode)->pde. As discussed in
Section 10.1.2,PROC_Ifinds theproc_inodeinstance associated with aninode(in the case ofprocinodes,
the inode data always immediately precede the VFS inode).

Once theproc_dir_entryinstance has been found, the routine registered for write purposes must be
invoked with suitable parameters — assuming, of course, that the routine exists and is not assigned a
null pointer.

How does the kernel implement a write routine forprocentries? This question is answered using
proc_write_foobar, which is included as an example for a write handler in the kernel sources:

kernel/Documentation/DocBook/procfs_example.c
static int proc_write_foobar(struct file *file,
const char *buffer,
unsigned long count,
void *data)
{
int len;
struct fb_data_t *fb_data = (struct fb_data_t *)data;

if(count > FOOBAR_LEN)
len = FOOBAR_LEN;
else
len = count;

if(copy_from_user(fb_data->value, buffer, len))
return -EFAULT;

fb_data->value[len] = ’\0’;
Free download pdf