Skip to contentSkip to conversation

EFAULT from file read.

DISCUSSION David Chow 3 participants 4 messages Sep 18, 2001 – Sep 19, 2001

lore
Share & tools
lore

David Chow

author
#0 lore raw

Dear all,

I am having trouble in reading a file in the kernel space using the file->f_op->read call, everything is ok. I start off file->f_pos = 0 . I also did a mntget to the super block before I call "file=dentry_open(.....)" . I intend to open the file in read only mode. What can be wrong? I have also check the inode->i_size is large enough for me to just read 8 bytes from the file. I keep having EFAULT error from the read call... also before calling mntget, also did a dget to the dentry. Any help is welcomed. Thanks.

regards,

David Chow

#1 lore raw
Quoted 11 lines · David Chow

Dear all,

I am having trouble in reading a file in the kernel space using the file->f_op->read call, everything is ok. I start off file->f_pos = 0 . I also did a mntget to the super block before I call "file=dentry_open(.....)" . I intend to open the file in read only mode. What can be wrong? I have also check the inode->i_size is large enough for me to just read 8 bytes from the file. I keep having EFAULT error from the read call... also before calling mntget, also did a dget to the dentry. Any help is welcomed. Thanks.

Quoted 4 lines

regards,

David Chow
-

Are you attempting file access in the kernel from a kernel-thread, or have you just decided to perform file access from some random kernel code?

File I/O requires a process context. Your file descriptor means nothing unless associated with the process that opened the file. The kernel, itself, is not a process. It functions in the process context of the caller. Also file I/O sleeps and pages. You need to understand this before you attempt to access files from within the kernel.

Normally, you don't access files from within the kernel. If you need file data for a driver for instance, you write an appropriate ioctl() function in your driver so a user-mode task can send the data to the driver. Your startup script inserts the module, then executes a program which initializes the module with the appropriate file data and initialization commands, all sent from user mode. Once the driver is initialized, the user-mode "starter" exits, freeing resources which would otherwise be trapped forever in your driver.

Cheers,
Dick Johnson

Penguin : Linux version 2.4.1 on an i686 machine (799.53 BogoMips).

I was going to compile a list of innovations that could be attributed to Microsoft. Once I realized that Ctrl-Alt-Del was handled in the BIOS, I found that there aren't any.

#2 lore raw
Quoted 2 lines · Richard B. Johnson

File I/O requires a process context. Your file descriptor means nothing unless associated with the process that opened the file.

It fscking doesn't. He had clearly said that he calls file->f_op->read(), which has nothing whatsofuckingever to descriptors. Sod off and don't return until you learn to read.

As for the original question - grep fro set_fs and you'll see what to do (basically, set_fs(KERNEL_DS) before the call of ->read() and restore afterwards).

author
#3 lore raw

Alexander Viro ¼g¹D¡G

Quoted 6 lines · Richard B. Johnson
Quoted 3 lines

File I/O requires a process context. Your file descriptor means nothing unless associated with the process that opened the file.

Quoted 7 lines

It fscking doesn't. He had clearly said that he calls file->f_op->read(), which has nothing whatsofuckingever to descriptors. Sod off and don't return until you learn to read.

As for the original question - grep fro set_fs and you'll see what to do (basically, set_fs(KERNEL_DS) before the call of ->read() and restore afterwards).

Problem solved by calling dummy=set_fs(KERNEL_DS) . But remember to call set_fs(dummy) to restore after the read.

regards,

David