kHTTPd
Main
Performance
Architecture
Kernel Tricks
kASYNCd
 


Links
RFC xxxx
Apache
Zeus
phHTTPd
Mindcraft
Netcraft
Oce Technologies
 
File Access

File access within the kernel is not very difficult. But make sure you thought it trough; For example, don't use configuration-files, a sysctl-variable is a much better and cleaner solution. Loading firmware is probably also not a good reason for reading files. There are cases where you definitly want to access a file (kHTTPd and knfsd are two of them), and although the code is there, I have not been able to find a simple example of how to do it. So here comes my example (note: It doesn't do any kernel-locking to keep the example simple):


void ReadFile(char *Filename, int StartPos)
{
	struct file 	*filp;
	char		*Buffer;
	mm_segment_t	oldfs;
	int		BytesRead;

	Buffer = kmalloc(4096,GFP_KERNEL);
	if (Buffer==NULL) 
		return;
	
	filp = filp_open(Filename,00,O_RDONLY);
	if (IS_ERR(filp)||(filp==NULL))
		return;  /* Or do something else */

	if (filp->f_op->read==NULL)
		return;  /* File(system) doesn't allow reads */

	/* Now read 4096 bytes from postion "StartPos" */
	filp->f_pos = StartPos;
	oldfs = get_fs();
	set_fs(KERNEL_DS);
	BytesRead = filp->f_op->read(filp,Buffer,4096,&filp->f_pos);
	set_fs(oldfs);

	/* Close the file */
	fput(filp);

}

The example copies the data from the buffercache to the kernel(thread)s private memory. If the copy is to costly for you, it is possible to use the buffercache directly. I will not give an example of this, as this is very complex, version-dependend and well documented in do_generic_file_read(...) in mm/filemap.c.