Re: [PATCH rust-next 2/2] rust: samples: miscdevice: add lseek samples
From: Benno Lossin
Date: Mon Aug 18 2025 - 18:06:03 EST
On Mon Aug 18, 2025 at 3:58 PM CEST, Ryosuke Yasuoka wrote:
> + fn llseek(me: Pin<&RustMiscDevice>, file: &File, offset: i64, whence: i32) -> Result<isize> {
> + dev_info!(me.dev, "LLSEEK Rust Misc Device Sample\n");
> + let pos: i64;
> + let eof: i64;
> +
> + // SAFETY:
> + // * The file is valid for the duration of this call.
> + // * f_inode must be valid while the file is valid.
> + unsafe {
> + pos = (*file.as_ptr()).f_pos;
> + eof = (*(*file.as_ptr()).f_inode).i_size;
> + }
Please include abstractions for writing & reading the file position
instead of using `unsafe`.
---
Cheers,
Benno
> +
> + let new_pos = match whence {
> + SEEK_SET => offset,
> + SEEK_CUR => pos + offset,
> + SEEK_END => eof + offset,
> + _ => {
> + dev_err!(me.dev, "LLSEEK does not recognised: {}.\n", whence);
> + return Err(EINVAL);
> + }
> + };
> +
> + if new_pos < 0 {
> + dev_err!(me.dev, "The file offset becomes negative: {}.\n", new_pos);
> + return Err(EINVAL);
> + }
> +
> + // SAFETY: The file is valid for the duration of this call.
> + let ret: isize = unsafe {
> + (*file.as_ptr()).f_pos = new_pos;
> + new_pos as isize
> + };
> +
> + Ok(ret)
> + }
> +
> fn ioctl(me: Pin<&RustMiscDevice>, _file: &File, cmd: u32, arg: usize) -> Result<isize> {
> dev_info!(me.dev, "IOCTLing Rust Misc Device Sample\n");
>