File io 如何编写只转发文件操作的Linux驱动程序?

File io 如何编写只转发文件操作的Linux驱动程序?,file-io,linux-kernel,kernel,driver,shfileoperation,File Io,Linux Kernel,Kernel,Driver,Shfileoperation,我需要实现一个Linux内核驱动程序,它(在第一步中)只将所有文件操作转发到另一个文件(在后面的步骤中,应该对其进行管理和操作,但我不想在这里讨论) 我的想法如下,但在读取时,内核崩溃: static struct { struct file *file; char *file_name; int open; } file_out_data = { .file_name = "/any_file", .open = 0, }; int memory_o

我需要实现一个Linux内核驱动程序,它(在第一步中)只将所有文件操作转发到另一个文件(在后面的步骤中,应该对其进行管理和操作,但我不想在这里讨论)

我的想法如下,但在读取时,内核崩溃:

static struct {
    struct file *file;
    char *file_name;
    int open;
} file_out_data = {
.file_name  = "/any_file",
.open       = 0, 
};


int memory_open(struct inode *inode, struct file *filp) {
  PRINTK("<1>open memory module\n");
    /* 
     * We don't want to talk to two processes at the same time 
     */
    if (file_out_data.open)
        return -EBUSY;
    /*
     * Initialize the message 
     */
    Message_Ptr = Message;
    try_module_get(THIS_MODULE);

        file_out_data.file = filp_open(file_out_data.file_name, filp->f_flags, filp->f_mode); //here should be another return handling in case of fail
    file_out_data.open++;

  /* Success */
  return 0;
}

int memory_release(struct inode *inode, struct file *filp) {
      PRINTK("<1>release memory module\n");
    /* 
     * We're now ready for our next caller 
     */
    file_out_data.open--;
    filp_close(file_out_data.file,NULL);

    module_put(THIS_MODULE);
  /* Success */
  return 0;
}

ssize_t memory_read(struct file *filp, char *buf, 
                    size_t count, loff_t *f_pos) { 
   PRINTK("<1>read memory module \n");
ret=file_out_data.file->f_op->read(file_out_data.file,buf,count,f_pos); //corrected one, false one is to find in the history
    return ret;

}
静态结构{
结构文件*文件;
字符*文件名;
int-open;
}文件输出数据={
.file_name=“/any_文件”,
.open=0,
};
int memory_open(结构索引节点*inode,结构文件*filp){
PRINTK(“打开内存模块”);
/* 
*我们不想同时与两个进程对话
*/
如果(文件输出数据打开)
返回-EBUSY;
/*
*初始化消息
*/
Message_Ptr=消息;
try_module_get(此_模块);
file_out_data.file=filp_open(file_out_data.file_name,filp->f_标志,filp->f_模式);//如果失败,这里应该有另一个返回处理
文件输出数据.open++;
/*成功*/
返回0;
}
int内存释放(结构索引节点*inode,结构文件*filp){
PRINTK(“释放内存模块”);
/* 
*我们现在准备好接下一个电话了
*/
文件_out_data.open--;
filp_close(file_out_data.file,NULL);
模块输出(该模块);
/*成功*/
返回0;
}
ssize_t memory_u read(结构文件*filp,字符*buf,
大小计数,loff\u t*f\u pos){
PRINTK(“读取内存模块\n”);
ret=file\u out\u data.file->f\u op->read(file\u out\u data.file,buf,count,f\u pos);//纠正一个,在历史记录中找到一个假
返回ret;
}

那么,有人能告诉我为什么吗?

为什么要递增
文件输出数据。打开
两次,递减一次?这可能会导致您在关闭后使用
文件输出数据。文件

为什么要递增
文件输出数据。打开两次,递减一次?这可能会导致您在关闭后使用
file\u out\u data.file

  • 不要使用
    set_fs()
    ,因为没有理由这样做
  • 使用
    file->f\u fop->read()
    而不是
    vfs\u read
    。请看一看结构和结构
  • 不要使用
    set_fs()
    ,因为没有理由这样做
  • 使用
    file->f\u fop->read()
    而不是
    vfs\u read
    。请看一看结构和结构

  • 您想在您读取的文件中写入内存吗? 因为你在阅读而不是写作。。。
    可能我错了

    您想在您读取的文件中写入内存吗? 因为你在阅读而不是写作。。。
    可能是我错了

    对不起,是我的错。是的,它应该只增加一次。但这并没有改变任何事情,因为它甚至在第一次崩溃。对不起,是我的错。是的,它应该只增加一次。但这并没有改变任何事情,因为它甚至在第一次崩溃。这只是一个比家庭作业大一点的项目的起点…但事实就是如此。如果你知道一种更有效的方法,我不能使用vfs命令。似乎没有理由在阅读时使用
    set\fs(get\ds())
    。我只是从。。。不要抄袭你不完全理解的东西。我认为它甚至被锁定在那里,应该可以正常工作。这只是一个比家庭作业大一点的项目的起点……但事实就是如此。如果你知道更有效的方法,我不能使用vfs命令。似乎没有理由在阅读时使用
    set\fs(get\ds())
    。我只是从。。。不要抄袭你不完全理解的东西。我想它甚至锁在那里,应该可以用。谢谢。在我阅读你的帖子之前,我试过“ret=file\u out\u data.file->f\u op->read(file\u out\u data.file,buf,count,f\u pos);”,效果不错。谢谢。在我读你的帖子之前,我试过“ret=file\u out\u data.file->f\u op->read(file\u out\u data.file,buf,count,f\u pos);”,结果成功了。