如何在Linux内核中从`struct inode *`打开和读取文件

Pio*_*dyk 4 c inode file linux-kernel

我想从 Linux Kernel v3.0.8 中检查文件的内容,只知道struct inode *. 我只需要读取这个 inode 指向的文件的开头,然后关闭并返回。我不关心文件名/挂载点等附加信息。事实上,文件可能没有名称(如已删除但仍打开)。是否可以?

Pio*_*dyk 5

我终于这样做了:

  1. 这是需要的。
struct path root;
struct file *filerd;
Run Code Online (Sandbox Code Playgroud)
  1. 获取 init 任务 fs root。
task_lock(&init_task);
get_fs_root(init_task.fs, &root);
task_unlock(&init_task);
Run Code Online (Sandbox Code Playgroud)
  1. 将 dentry 更改为此文件:
root.dentry = d_find_alias(inode);
Run Code Online (Sandbox Code Playgroud)
  1. 打开文件:
filerd = file_open_root(root.dentry->d_parent, root.mnt,
                        root.dentry->d_name.name, O_RDONLY);
Run Code Online (Sandbox Code Playgroud)

它适用于我测试的每个进程和不同的挂载点,这让我感到惊讶。