0

File handling in a kernel module. What is the appropriate way to 'seek' in a file? I could not find a sys_seek() function (such as sys_read()). Or, is it better only to use the VFS function API?

The question How to read/write files within a Linux kernel module? does not handle input stream navigation, i.e. there is no reference to tell()/seek() of any kind.

Frank-Rene Schäfer
  • 3,182
  • 27
  • 51

1 Answers1

1

seek functionality in the kernel space can be achieved by vfs_llseek function:

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

The function returns resulted offset or negative value in case of error.

Tsyvarev
  • 60,011
  • 17
  • 110
  • 153
  • I would feel bad to use APIs of different levels of abstraction. – Frank-Rene Schäfer Oct 20 '18 at 05:56
  • `vfs_llseek` has the same "level of abstraction" as a function `vfs_write`, noted in the [answer](https://stackoverflow.com/a/1184346/3440745) to the question referenced by yours. If you compare with `sys_read` instead... then symply do not use `sys_*` functions (syscalls) in the kernel: syscalls are primarily for user-space applications. – Tsyvarev Oct 20 '18 at 12:22
  • So, I guess you want to say that my assumption that kernel modules better rely on the VFS functions instead of syscalls is correct? – Frank-Rene Schäfer Oct 20 '18 at 17:29
  • Yes, `vfs_` prefixed functions are better for the Linux kernel programming than `sys_` ones. E.g. file-related syscalls work with a **file descriptor** (`int fd`), but kernel itself mostly uses an object of type `struct file*`. Even if you have a *file descriptor* as input, then you may use [ksys_lseek](https://elixir.bootlin.com/linux/v4.18.16/source/fs/read_write.c#L304). – Tsyvarev Oct 20 '18 at 17:59