Rust for Linux List
 help / color / mirror / Atom feed
* [PATCH] rust: Add functions to create and modify an Inode
@ 2023-04-13 14:01 Ariel Miculas
  2023-04-13 18:36 ` Miguel Ojeda
  0 siblings, 1 reply; 6+ messages in thread
From: Ariel Miculas @ 2023-04-13 14:01 UTC (permalink / raw)
  To: rust-for-linux; +Cc: Ariel Miculas

Add try_new_inode function associated to SuperBlock which allocates a
new inode and implement a few setter functions for INode.

Signed-off-by: Ariel Miculas <amiculas@cisco.com>
---
 rust/kernel/fs.rs | 41 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 41 insertions(+)

diff --git a/rust/kernel/fs.rs b/rust/kernel/fs.rs
index 1ba01629e9cd..58c281123c28 100644
--- a/rust/kernel/fs.rs
+++ b/rust/kernel/fs.rs
@@ -707,6 +707,18 @@ pub struct SuperBlock<T: Type + ?Sized>(
     PhantomData<T>,
 );
 
+impl<T: Type + ?Sized> SuperBlock<T> {
+    /// Create a new inode
+    pub fn try_new_inode(&self) -> Result<INode> {
+        let inode = unsafe { bindings::new_inode(self.0.get()) };
+        if inode.is_null() {
+            return Err(ENOMEM);
+        }
+        // SAFETY: We've just allocated a new inode and checked that it's not null
+        Ok(INode(UnsafeCell::new(unsafe { *inode })))
+    }
+}
+
 /// Wraps the kernel's `struct inode`.
 ///
 /// # Invariants
@@ -716,6 +728,35 @@ pub struct SuperBlock<T: Type + ?Sized>(
 #[repr(transparent)]
 pub struct INode(pub(crate) UnsafeCell<bindings::inode>);
 
+impl INode {
+    /// set the field i_mode of the inode
+    pub fn set_mode(&mut self, mode: u16) {
+        self.0.get_mut().i_mode = mode;
+    }
+    /// set the field i_ino of the inode
+    pub fn set_ino(&mut self, ino: u64) {
+        self.0.get_mut().i_ino = ino;
+    }
+
+    /// set the current time in the atime, ctime and mtime fields of the inode
+    pub fn set_current_time(&mut self) {
+        let time = unsafe { bindings::current_time(self.0.get()) };
+        self.0.get_mut().i_atime = time;
+        self.0.get_mut().i_ctime = time;
+        self.0.get_mut().i_mtime = time;
+    }
+
+    /// set the field i_fop of the inode
+    pub fn set_fop(&mut self, fop: *const bindings::file_operations) {
+        self.0.get_mut().__bindgen_anon_3.i_fop = fop;
+    }
+
+    /// set the field i_op of the inode
+    pub fn set_op(&mut self, fop: *const bindings::inode_operations) {
+        self.0.get_mut().i_op = fop;
+    }
+}
+
 // SAFETY: The type invariants guarantee that `INode` is always ref-counted.
 unsafe impl AlwaysRefCounted for INode {
     fn inc_ref(&self) {
-- 
2.40.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2023-04-18 23:57 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-04-13 14:01 [PATCH] rust: Add functions to create and modify an Inode Ariel Miculas
2023-04-13 18:36 ` Miguel Ojeda
2023-04-13 18:59   ` Ariel Miculas
2023-04-13 19:40     ` Wedson Almeida Filho
2023-04-13 19:59       ` Ariel Miculas
     [not found]   ` <CAPDJoNt_jCPfkmS7pRVA=1-U6csPEMuYsjTdwYm3HpUyZBVEUQ@mail.gmail.com>
2023-04-18 23:57     ` Miguel Ojeda

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox