Results 1 to 1 of 1
I'm trying to make a driver which block write operations directly on a block device file( /dev/hdxy).
I tried to start with this code:
Code:
#include <linux/kernel.h>
#include <linux/init.h>
#include ...
- 03-27-2010 #1Just Joined!
- Join Date
- Mar 2010
- Posts
- 1
block device driver
I'm trying to make a driver which block write operations directly on a block device file( /dev/hdxy).
I tried to start with this code:
Code:#include <linux/kernel.h> #include <linux/init.h> #include <linux/module.h> #include <linux/fs.h> #include <linux/blkdev.h> MODULE_DESCRIPTION("test module"); MODULE_AUTHOR("vali"); MODULE_LICENSE("GPL"); struct block_device *bd; struct file_operations *saved_fop; ssize_t my_write(struct file *file, const char __user *data, size_t size, loff_t *offset) { printk(KERN_DEBUG "intercept block write\n"); return saved_fop->write(file, data, size, offset); } int test_init(void) { struct file_operations *fop; bd = lookup_bdev("/dev/hdb1"); saved_fop = (struct file_operations *)bd->bd_inode->i_fop; fop = (struct file_operations *)kmalloc(sizeof(*fop), GFP_KERNEL); memcpy(fop, saved_fop, sizeof(struct file_operations)); fop->write = my_write; bd->bd_inode->i_fop = fop; printk(KERN_DEBUG "load module\n"); return 0; } void test_exit(void) { kfree(bd->bd_inode->i_fop); bd->bd_inode->i_fop = saved_fop; printk(KERN_DEBUG "exit module\n"); } module_init(test_init); module_exit(test_exit);
When I run "dd if=/dev/zero bs=512 count=10 of=/dev/hdb1" command I expect to get some "intercept block write" message(s).
But I only get "load module" when the module in loaded.
What is wrong ? Why my_write is not executed ?
Thanks! Any help is appreciated.


Reply With Quote
