Skip to main content

vmi_os_linux/comps/
vfsmount.rs

1use vmi_core::{Va, VmiError, VmiState, VmiVa, driver::VmiRead};
2
3use super::{LinuxDEntry, macros::impl_offsets};
4use crate::{ArchAdapter, LinuxError, LinuxOs};
5
6/// A Linux VFS mount struct.
7///
8/// The `vfsmount` structure is used to represent a mounted filesystem instance
9/// in the Linux Virtual Filesystem (VFS).
10///
11/// # Implementation Details
12///
13/// Corresponds to `vfsmount`.
14pub struct LinuxVFSMount<'a, Driver>
15where
16    Driver: VmiRead,
17    Driver::Architecture: ArchAdapter<Driver>,
18{
19    /// The VMI state.
20    vmi: VmiState<'a, LinuxOs<Driver>>,
21
22    /// The virtual address of the `vfsmount` structure.
23    va: Va,
24}
25
26impl<Driver> VmiVa for LinuxVFSMount<'_, Driver>
27where
28    Driver: VmiRead,
29    Driver::Architecture: ArchAdapter<Driver>,
30{
31    fn va(&self) -> Va {
32        self.va
33    }
34}
35
36impl<'a, Driver> LinuxVFSMount<'a, Driver>
37where
38    Driver: VmiRead,
39    Driver::Architecture: ArchAdapter<Driver>,
40{
41    impl_offsets!();
42
43    /// Creates a new VFS mount.
44    pub fn new(vmi: VmiState<'a, LinuxOs<Driver>>, va: Va) -> Self {
45        Self { vmi, va }
46    }
47
48    /// Returns the root directory entry (`dentry`) of the mount.
49    ///
50    /// `mnt_root` points to the root directory (`/`) of the mounted filesystem.
51    /// It is a `dentry` (directory entry) that represents the starting point of
52    /// the filesystem. Every mount point has a `mnt_root`, but this root does
53    /// not necessarily have to be `/` (e.g., for a chroot environment, the root
54    /// could be `/home/user`).
55    ///
56    /// # Implementation Details
57    ///
58    /// Corresponds to `vfsmount->mnt_root`.
59    pub fn mnt_root(&self) -> Result<LinuxDEntry<'a, Driver>, VmiError> {
60        let offsets = self.offsets();
61        let __vfsmount = &offsets.vfsmount;
62
63        let mnt_root = self
64            .vmi
65            .read_va_native(self.va + __vfsmount.mnt_root.offset())?;
66
67        if mnt_root.is_null() {
68            return Err(LinuxError::CorruptedStruct("vfsmount->mnt_root").into());
69        }
70
71        Ok(LinuxDEntry::new(self.vmi, mnt_root))
72    }
73}