|
| 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 | +} |
0 commit comments