简体   繁体   English

Linux VFS:vfs_read(…,&offset)与vfs_llseek(…,pos):如何从内核模块中查找文件

[英]Linux VFS: vfs_read(…, &offset) vs. vfs_llseek(…, pos): How to seek in a file from kernel module

GIVEN : 给予

in fs/read_write.c the function vfs_read() has a signature as shown bewlow: fs/read_write.c vfs_read() ,函数vfs_read()具有如下所示的签名:

 ssize_t vfs_read(struct file *file, char __user *buf, size_t count, loff_t *pos)

where the last argument is a pointer to a position. 最后一个参数是指向位置的指针。 The signature itself is not documented. 签名本身未记录。 The signature, I would think, insinuates that the initial read position is passed to the read function, and maybe the updated position (next position to be read from) is written in *pos . 我认为,签名暗示了将初始读取位置传递给读取功能,并且可能将更新后的位置(要读取的下一个位置)写入*pos Then, however, there is the function vfs_llseek() in fs/read_write.c with the signature 但是,然后在fs/read_write.c vfs_llseek()fs/read_write.c带有签名的函数vfs_llseek()

loff_t vfs_llseek(struct file *file, loff_t offset, int whence)                                                                              

which looks like a 'normal' seek function. 看起来像是“正常”搜索功能。 Also, the signature is not documented along side with the code. 另外,签名未随代码一起记录。

QUESTIONS : 问题

  1. How does one actually seek in a file using VFS? 如何使用VFS实际上在文件中查找?
  2. Is the position equal to the index of the byte in the file, ie first byte position = 0, position = position + 1 for subsequent bytes? 位置是否等于文件中字节的索引,即第一个字节的位置= 0,后续的字节的位置=位置+1?

How does one actually seek in a file using VFS? 如何使用VFS实际上在文件中查找?

Using vfs_llseek is correct for this purpose. 为此,使用vfs_llseek是正确的。

VFS actually has a notion about current file's position , which is stored in the file->f_pos field. VFS实际上有一个关于当前文件位置的概念,该概念存储在file->f_pos字段中。 Successful call for vfs_llseek updates this position. 成功调用vfs_llseek更新此职位。 But for use this position when read the file, you need explicitly pass the value of the field via pos parameter of the vfs_read and, upon successful return, write resulted value of that parameter back to the field: 但是要在读取文件时使用此位置,您需要通过vfs_read pos参数显式传递字段的值,并在成功返回后将该参数的结果值写回该字段:

loff_t pos = file->f_pos;
ssize_t res = vfs_read(file, buf, count, &pos);
if (res > 0) {
  file->f_pos = pos;
}

Existed signature of vfs_read allows to use this function both in normal read syscall and in pread syscall, which uses user-specified position instead of the current one. vfs_read签名允许在正常read syscall和pread syscall中使用此功能,后者使用用户指定的位置而不是当前位置。

Is the position equal to the index of the byte in the file, ie first byte position = 0, position = position + 1 for subsequent bytes? 位置是否等于文件中字节的索引,即第一个字节的位置= 0,后续的字节的位置=位置+1?

This is true for regular files, which are stored on a hard drive or other media. 对于存储在硬盘驱动器或其他介质上的常规文件,这是正确的。

For special files, like ones located under /sys or /dev , meaning of the position could be any (that is, it is defined by the file). 对于特殊文件,例如位于/sys/dev下的文件,该位置的含义可以是任意的 (即,由文件定义)。 Eg, if a file exposes information as an array of records, position could mean the index of the record, not the index of the byte. 例如,如果文件将信息公开为记录的数组,则位置可能表示记录的索引,而不是字节的索引。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM