From: Ryosuke Yasuoka <ryasuoka@redhat.com>
To: arnd@arndb.de, gregkh@linuxfoundation.org, ojeda@kernel.org,
alex.gaynor@gmail.com, boqun.feng@gmail.com, gary@garyguo.net,
bjorn3_gh@protonmail.com, lossin@kernel.org,
a.hindborg@kernel.org, aliceryhl@google.com, tmgross@umich.edu,
dakr@kernel.org, viro@zeniv.linux.org.uk, brauner@kernel.org,
jack@suse.cz
Cc: Ryosuke Yasuoka <ryasuoka@redhat.com>,
rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org,
linux-fsdevel@vger.kernel.org
Subject: [PATCH rust-next v2 1/3] rust: fs: add pos/pos_mut methods for LocalFile struct
Date: Wed, 15 Oct 2025 13:02:41 +0900 [thread overview]
Message-ID: <20251015040246.151141-2-ryasuoka@redhat.com> (raw)
In-Reply-To: <20251015040246.151141-1-ryasuoka@redhat.com>
Add pos() and pos_mut() methods to get and set a file position.
Signed-off-by: Ryosuke Yasuoka <ryasuoka@redhat.com>
---
rust/kernel/fs/file.rs | 61 ++++++++++++++++++++++++++++++++++++++++++
1 file changed, 61 insertions(+)
diff --git a/rust/kernel/fs/file.rs b/rust/kernel/fs/file.rs
index cf06e73a6da0..bda6cc540dfe 100644
--- a/rust/kernel/fs/file.rs
+++ b/rust/kernel/fs/file.rs
@@ -283,6 +283,23 @@ pub unsafe fn from_raw_file<'a>(ptr: *const bindings::file) -> &'a LocalFile {
unsafe { &*ptr.cast() }
}
+ /// Create a mutable reference to a [`LocalFile`] from a valid pointer.
+ ///
+ /// # Safety
+ ///
+ /// * The caller must ensure that `ptr` points at a valid file and that the file's refcount is
+ /// positive for the duration of `'a`.
+ /// * The caller must ensure that if there is an active call to `fdget_pos` that did not take
+ /// the `f_pos_lock` mutex, then that call is on the current thread.
+ #[inline]
+ pub unsafe fn from_raw_file_mut<'a>(ptr: *mut bindings::file) -> &'a mut LocalFile {
+ // SAFETY: The caller guarantees that the pointer is not dangling and stays valid for the
+ // duration of `'a`. The cast is okay because `LocalFile` is `repr(transparent)`.
+ //
+ // INVARIANT: The caller guarantees that there are no problematic `fdget_pos` calls.
+ unsafe { &mut *ptr.cast() }
+ }
+
/// Assume that there are no active `fdget_pos` calls that prevent us from sharing this file.
///
/// This makes it safe to transfer this file to other threads. No checks are performed, and
@@ -337,6 +354,20 @@ pub fn flags(&self) -> u32 {
// FIXME(read_once): Replace with `read_once` when available on the Rust side.
unsafe { core::ptr::addr_of!((*self.as_ptr()).f_flags).read_volatile() }
}
+
+ /// Get the current `f_pos` with the file.
+ #[inline]
+ pub fn pos(&self) -> i64 {
+ // SAFETY: The `f_pos` is valid while `LocalFile` is valid
+ unsafe { (*self.as_ptr()).f_pos }
+ }
+
+ /// Get a mutable reference to the `f_pos`.
+ #[inline]
+ pub fn pos_mut(&mut self) -> &mut i64 {
+ // SAFETY: The `f_pos` is valid while `LocalFile` is valid
+ unsafe { &mut (*self.as_ptr()).f_pos }
+ }
}
impl File {
@@ -356,6 +387,23 @@ pub unsafe fn from_raw_file<'a>(ptr: *const bindings::file) -> &'a File {
// INVARIANT: The caller guarantees that there are no problematic `fdget_pos` calls.
unsafe { &*ptr.cast() }
}
+
+ /// Creates a mutable reference to a [`File`] from a valid pointer.
+ ///
+ /// # Safety
+ ///
+ /// * The caller must ensure that `ptr` points at a valid file and that the file's refcount is
+ /// positive for the duration of `'a`.
+ /// * The caller must ensure that if there are active `fdget_pos` calls on this file, then they
+ /// took the `f_pos_lock` mutex.
+ #[inline]
+ pub unsafe fn from_raw_file_mut<'a>(ptr: *mut bindings::file) -> &'a mut File {
+ // SAFETY: The caller guarantees that the pointer is not dangling and stays valid for the
+ // duration of `'a`. The cast is okay because `File` is `repr(transparent)`.
+ //
+ // INVARIANT: The caller guarantees that there are no problematic `fdget_pos` calls.
+ unsafe { &mut *ptr.cast() }
+ }
}
// Make LocalFile methods available on File.
@@ -372,6 +420,19 @@ fn deref(&self) -> &LocalFile {
}
}
+// Make LocalFile methods available on File.
+impl core::ops::DerefMut for File {
+ #[inline]
+ fn deref_mut(&mut self) -> &mut Self::Target {
+ // SAFETY: The caller provides a `&File`, and since it is a reference, it must point at a
+ // valid file for the desired duration.
+ //
+ // By the type invariants, there are no `fdget_pos` calls that did not take the
+ // `f_pos_lock` mutex.
+ unsafe { LocalFile::from_raw_file_mut(core::ptr::from_mut(self).cast()) }
+ }
+}
+
/// A file descriptor reservation.
///
/// This allows the creation of a file descriptor in two steps: first, we reserve a slot for it,
--
2.51.0
next prev parent reply other threads:[~2025-10-15 4:03 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-10-15 4:02 [PATCH rust-next v2 0/3] rust: miscdevice: add llseek support Ryosuke Yasuoka
2025-10-15 4:02 ` Ryosuke Yasuoka [this message]
2025-10-15 4:02 ` [PATCH rust-next v2 2/3] " Ryosuke Yasuoka
2025-10-15 4:02 ` [PATCH rust-next v2 3/3] rust: samples: miscdevice: add lseek samples Ryosuke Yasuoka
2025-10-15 5:40 ` [PATCH rust-next v2 0/3] rust: miscdevice: add llseek support Greg KH
2025-10-16 10:19 ` Ryosuke Yasuoka
2025-10-16 11:24 ` Greg KH
2025-10-16 14:39 ` Ryosuke Yasuoka
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20251015040246.151141-2-ryasuoka@redhat.com \
--to=ryasuoka@redhat.com \
--cc=a.hindborg@kernel.org \
--cc=alex.gaynor@gmail.com \
--cc=aliceryhl@google.com \
--cc=arnd@arndb.de \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun.feng@gmail.com \
--cc=brauner@kernel.org \
--cc=dakr@kernel.org \
--cc=gary@garyguo.net \
--cc=gregkh@linuxfoundation.org \
--cc=jack@suse.cz \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=lossin@kernel.org \
--cc=ojeda@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=tmgross@umich.edu \
--cc=viro@zeniv.linux.org.uk \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox