[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <DC5W7UQHOJSG.310L4WJ0AKZFE@kernel.org>
Date: Tue, 19 Aug 2025 00:05:41 +0200
From: "Benno Lossin" <lossin@...nel.org>
To: "Ryosuke Yasuoka" <ryasuoka@...hat.com>, <arnd@...db.de>,
<gregkh@...uxfoundation.org>, <ojeda@...nel.org>, <alex.gaynor@...il.com>,
<boqun.feng@...il.com>, <gary@...yguo.net>, <bjorn3_gh@...tonmail.com>,
<a.hindborg@...nel.org>, <aliceryhl@...gle.com>, <tmgross@...ch.edu>,
<dakr@...nel.org>, <lee@...nel.org>
Cc: <rust-for-linux@...r.kernel.org>, <linux-kernel@...r.kernel.org>
Subject: Re: [PATCH rust-next 2/2] rust: samples: miscdevice: add lseek
samples
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");
>
Powered by blists - more mailing lists