Skip to content

Commit a8819ab

Browse files
authored
Merge pull request raspberrypi#810 from wedsonaf/inode-dentry
rust: add definitions for ref-counted inodes and dentries
2 parents f7b260a + bebe4cd commit a8819ab

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed

rust/helpers.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,12 @@ void rust_helper___INIT_WORK_WITH_KEY(struct work_struct *work,
627627
}
628628
EXPORT_SYMBOL_GPL(rust_helper___INIT_WORK_WITH_KEY);
629629

630+
struct dentry *rust_helper_dget(struct dentry *dentry)
631+
{
632+
return dget(dentry);
633+
}
634+
EXPORT_SYMBOL_GPL(rust_helper_dget);
635+
630636
/*
631637
* We use `bindgen`'s `--size_t-is-usize` option to bind the C `size_t` type
632638
* as the Rust `usize` type, so we can use it in contexts where Rust

rust/kernel/fs.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
//! File systems.
4+
//!
5+
//! C headers: [`include/linux/fs.h`](../../../../include/linux/fs.h)
6+
7+
use crate::{bindings, AlwaysRefCounted};
8+
use core::{cell::UnsafeCell, ptr};
9+
10+
/// Wraps the kernel's `struct inode`.
11+
///
12+
/// # Invariants
13+
///
14+
/// Instances of this type are always ref-counted, that is, a call to `ihold` ensures that the
15+
/// allocation remains valid at least until the matching call to `iput`.
16+
#[repr(transparent)]
17+
pub struct INode(pub(crate) UnsafeCell<bindings::inode>);
18+
19+
// SAFETY: The type invariants guarantee that `INode` is always ref-counted.
20+
unsafe impl AlwaysRefCounted for INode {
21+
fn inc_ref(&self) {
22+
// SAFETY: The existence of a shared reference means that the refcount is nonzero.
23+
unsafe { bindings::ihold(self.0.get()) };
24+
}
25+
26+
unsafe fn dec_ref(obj: ptr::NonNull<Self>) {
27+
// SAFETY: The safety requirements guarantee that the refcount is nonzero.
28+
unsafe { bindings::iput(obj.cast().as_ptr()) }
29+
}
30+
}
31+
32+
/// Wraps the kernel's `struct dentry`.
33+
///
34+
/// # Invariants
35+
///
36+
/// Instances of this type are always ref-counted, that is, a call to `dget` ensures that the
37+
/// allocation remains valid at least until the matching call to `dput`.
38+
#[repr(transparent)]
39+
pub struct DEntry(pub(crate) UnsafeCell<bindings::dentry>);
40+
41+
// SAFETY: The type invariants guarantee that `DEntry` is always ref-counted.
42+
unsafe impl AlwaysRefCounted for DEntry {
43+
fn inc_ref(&self) {
44+
// SAFETY: The existence of a shared reference means that the refcount is nonzero.
45+
unsafe { bindings::dget(self.0.get()) };
46+
}
47+
48+
unsafe fn dec_ref(obj: ptr::NonNull<Self>) {
49+
// SAFETY: The safety requirements guarantee that the refcount is nonzero.
50+
unsafe { bindings::dput(obj.cast().as_ptr()) }
51+
}
52+
}

rust/kernel/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ pub mod device;
5151
pub mod driver;
5252
pub mod error;
5353
pub mod file;
54+
pub mod fs;
5455
pub mod gpio;
5556
pub mod hwrng;
5657
pub mod irq;

0 commit comments

Comments
 (0)