Skip to content

Commit b5e5fb8

Browse files
committed
rust: fs: introduce INode<T>
It's an inode from filesystem type `T`. This is preparation for creating new inodes (for example, to create the root inode of a new superblock), which will come in a subsequent commit. Signed-off-by: Wedson Almeida Filho <walmeida@microsoft.com>
1 parent a8ed2bc commit b5e5fb8

2 files changed

Lines changed: 55 additions & 2 deletions

File tree

rust/helpers.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <linux/build_bug.h>
2626
#include <linux/err.h>
2727
#include <linux/errname.h>
28+
#include <linux/fs.h>
2829
#include <linux/mutex.h>
2930
#include <linux/refcount.h>
3031
#include <linux/sched/signal.h>
@@ -144,6 +145,12 @@ struct kunit *rust_helper_kunit_get_current_test(void)
144145
}
145146
EXPORT_SYMBOL_GPL(rust_helper_kunit_get_current_test);
146147

148+
off_t rust_helper_i_size_read(const struct inode *inode)
149+
{
150+
return i_size_read(inode);
151+
}
152+
EXPORT_SYMBOL_GPL(rust_helper_i_size_read);
153+
147154
/*
148155
* `bindgen` binds the C `size_t` type as the Rust `usize` type, so we can
149156
* use it in contexts where Rust expects a `usize` like slice (array) indices.

rust/kernel/fs.rs

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
//! C headers: [`include/linux/fs.h`](../../include/linux/fs.h)
88
99
use crate::error::{code::*, from_result, to_result, Error, Result};
10-
use crate::types::Opaque;
10+
use crate::types::{AlwaysRefCounted, Opaque};
1111
use crate::{bindings, init::PinInit, str::CStr, try_pin_init, ThisModule};
12-
use core::{marker::PhantomData, marker::PhantomPinned, pin::Pin};
12+
use core::{marker::PhantomData, marker::PhantomPinned, pin::Pin, ptr};
1313
use macros::{pin_data, pinned_drop};
1414

1515
/// Maximum size of an inode.
@@ -91,6 +91,52 @@ impl PinnedDrop for Registration {
9191
}
9292
}
9393

94+
/// A node in the file system index (inode).
95+
///
96+
/// Wraps the kernel's `struct inode`.
97+
///
98+
/// # Invariants
99+
///
100+
/// Instances of this type are always ref-counted, that is, a call to `ihold` ensures that the
101+
/// allocation remains valid at least until the matching call to `iput`.
102+
#[repr(transparent)]
103+
pub struct INode<T: FileSystem + ?Sized>(Opaque<bindings::inode>, PhantomData<T>);
104+
105+
impl<T: FileSystem + ?Sized> INode<T> {
106+
/// Returns the number of the inode.
107+
pub fn ino(&self) -> u64 {
108+
// SAFETY: `i_ino` is immutable, and `self` is guaranteed to be valid by the existence of a
109+
// shared reference (&self) to it.
110+
unsafe { (*self.0.get()).i_ino }
111+
}
112+
113+
/// Returns the super-block that owns the inode.
114+
pub fn super_block(&self) -> &SuperBlock<T> {
115+
// SAFETY: `i_sb` is immutable, and `self` is guaranteed to be valid by the existence of a
116+
// shared reference (&self) to it.
117+
unsafe { &*(*self.0.get()).i_sb.cast() }
118+
}
119+
120+
/// Returns the size of the inode contents.
121+
pub fn size(&self) -> i64 {
122+
// SAFETY: `self` is guaranteed to be valid by the existence of a shared reference.
123+
unsafe { bindings::i_size_read(self.0.get()) }
124+
}
125+
}
126+
127+
// SAFETY: The type invariants guarantee that `INode` is always ref-counted.
128+
unsafe impl<T: FileSystem + ?Sized> AlwaysRefCounted for INode<T> {
129+
fn inc_ref(&self) {
130+
// SAFETY: The existence of a shared reference means that the refcount is nonzero.
131+
unsafe { bindings::ihold(self.0.get()) };
132+
}
133+
134+
unsafe fn dec_ref(obj: ptr::NonNull<Self>) {
135+
// SAFETY: The safety requirements guarantee that the refcount is nonzero.
136+
unsafe { bindings::iput(obj.cast().as_ptr()) }
137+
}
138+
}
139+
94140
/// A file system super block.
95141
///
96142
/// Wraps the kernel's `struct super_block`.

0 commit comments

Comments
 (0)