Rust for Linux List
 help / color / mirror / Atom feed
From: Ariel Miculas <ariel.miculas@gmail.com>
To: rust-for-linux@vger.kernel.org
Cc: Ariel Miculas <amiculas@cisco.com>
Subject: [PATCH] rust: Add functions to create and modify an Inode
Date: Thu, 13 Apr 2023 17:01:38 +0300	[thread overview]
Message-ID: <20230413140138.348137-1-amiculas@cisco.com> (raw)

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


             reply	other threads:[~2023-04-13 14:02 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-04-13 14:01 Ariel Miculas [this message]
2023-04-13 18:36 ` [PATCH] rust: Add functions to create and modify an Inode 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

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=20230413140138.348137-1-amiculas@cisco.com \
    --to=ariel.miculas@gmail.com \
    --cc=amiculas@cisco.com \
    --cc=rust-for-linux@vger.kernel.org \
    /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