I have created block device in kernel module. When some I/O happens I read/write all data from/to another existing device (let's say /dev/sdb
).
It opens OK, but read/write operations return 14 error(EFAULT
,Bad Address). After some research I found that I need map address to user space(probably buffer
or filp
variables), but copy_to_user
function does not help. Also I looked to mmap()
and remap_pfn_range()
functions, but I can not get how to use them in my code, especially where to get correct vm_area_struct
structure. All examples that I found, used char devices and file_operations
structure, not block device.
Any hints? Thanks for help.
Here is my code for reading:
mm_segment_t old_fs;
old_fs = get_fs();
set_fs(KERNEL_DS);
filp = filp_open("/dev/sdb", O_RDONLY | O_DIRECT | O_SYNC, 00644);
if(IS_ERR(filp))
{
set_fs(old_fs);
int err = PTR_ERR(filp);
printk(KERN_ALERT"Can not open file - %d", err);
return;
}
else
{
bytesRead = vfs_read(filp, buffer, nbytes, &offset); //It gives 14 error
filp_close(filp, NULL);
}
set_fs(old_fs);