file operations in kernel mode

Submitted by ananth
on February 13, 2005 - 12:43am

Hi,
I wanted to read from a file a line at a time & fill my data structure in my kernel module.I want something like fgets.
1 more thing.I have intercepted open system call.If the opened file is in write/append mode and if that file is in my datastructure,i want to make a copy of it.How to do it?please help me
1 way i got was to use filp function(which is exported into kernel mode)But that fails if flags=33345(which mode is it?i think its O_WRONLY|O_CREAT|O_TRUNC).
Please suggest some easy way of doing file operations in kernel mode.
Ananth
NITK

filp_open

on
February 17, 2005 - 11:55am

filp = filp_open();

oldfs = get_fs();
set_fs(KERNEL_DS);
filp->f_op->read();
filp->f_op->write();
set_fs(oldfs);

filp_close(filp, NULL);

I wonder why you get those strange flags, it looks too large.

filp_open, f->f_op->read, error "-14"

halil agin (not verified)
on
December 8, 2005 - 6:06am

Hello,

I wrote the below code, and install to kernel as a module. /etc/aa file exists. And i can open it correctly. But return of f->f_op->read is "-14". Why does this happen?

thank you,

Halil agin,

===================================================
f = filp_open("/etc/aa",O_RDONLY,0);
f->f_pos = 0;

if (f->f_op->read) {
readret = f->f_op->read(f, buffer, 64, f->f_pos);
printk("DEBUG : readret : %d\n", readret);
buffer[64]='\0';
printk("DEBUG : ----%s---\n", buffer);
}
===================================================

hi, the call fails because

Anonymous (not verified)
on
January 17, 2007 - 11:09am

hi,
the call fails because read expects the buffer to be in user space, not kernel space

Re: filp_open, f->f_op->read, error "-14"

uJlbu4 (not verified)
on
June 13, 2009 - 9:26am

It seems that you didn't allocate memory for "buffer" variable...

Opening/Writing files in

on
January 17, 2007 - 1:05pm

Opening/Writing files in kernel space is a very very bad idea. The kernel doesn't have a context of it's own, it runs on behalf of a user context. You should probably re-analyze your problem and try to figure out some alternate approach. This might help -

http://kernelnewbies.org/FAQ/WhyWritingFilesFromKernelIsBad

./prabir

possible panic

Anish Bhatt (not verified)
on
May 16, 2007 - 7:15am

On an off note, it should be readret = f->f_op->read(f, buffer, 64, &f->f_pos);
The missing ampersand before f->f_pos might cause a kernel panic.

I want to get imformation of

birdfly (not verified)
on
July 15, 2007 - 9:39pm

I want to get imformation of /proc/net/arp in kernel space. how can I do? thanks.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.