Skip to content

Commit

Permalink
kernel: Rework parts of vfs
Browse files Browse the repository at this point in the history
  • Loading branch information
Lockna committed Sep 19, 2024
1 parent 7cb71f8 commit 39ca2e5
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 156 deletions.
1 change: 1 addition & 0 deletions kernel/src/fs/initramfs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// TODO: Add initramfs folder to archive and pass it to limine. Parse the archive here and add the folder and files to the tmpfs
18 changes: 16 additions & 2 deletions kernel/src/fs/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
//! The design and implementation of this virtual file system is heavily influenced by BSD.
use core::{error::Error, fmt::Display};

#[derive(Debug)]
pub enum Error {
pub enum VfsError {
VNodeNotFound,
NotADirectory,
IsADirectory,
Expand All @@ -12,7 +14,15 @@ pub enum Error {
FileSystemNotFound,
}

pub type Result<T, E = Error> = core::result::Result<T, E>;
impl Display for VfsError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
todo!()
}
}

impl Error for VfsError {}

pub type Result<T, E = VfsError> = core::result::Result<T, E>;

pub mod file;
mod mount;
Expand All @@ -21,3 +31,7 @@ pub mod tmpfs;
pub mod vfs;
pub mod vfs_syscalls;
pub mod vnode;

// todo: create fs init function which initializes vfs and initramfs

pub fn init() {}
44 changes: 3 additions & 41 deletions kernel/src/fs/mount.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,6 @@ impl Mount {
self.mnt_op_data.lock().vfs_root()
}

pub fn vfs_quotactl(&self) {
self.mnt_op_data.lock().vfs_quotactl()
}

pub fn vfs_statvfs(&self) {
self.mnt_op_data.lock().vfs_statvfs()
}
Expand All @@ -60,14 +56,6 @@ impl Mount {
self.mnt_op_data.lock().vfs_lookup(path)
}

pub fn vfs_fhtovp(&self) {
self.mnt_op_data.lock().vfs_fhtovp()
}

pub fn vfs_vptofh(&self) {
self.mnt_op_data.lock().vfs_vptofh()
}

pub fn vfs_init(&mut self) {
self.mnt_op_data.lock().vfs_init()
}
Expand All @@ -76,10 +64,6 @@ impl Mount {
self.mnt_op_data.lock().vfs_done()
}

pub fn vfs_extattrctl(&self) {
self.mnt_op_data.lock().vfs_extattrctl()
}

pub fn vfs_name(&self) -> String {
self.mnt_op_data.lock().vfs_name()
}
Expand All @@ -100,14 +84,9 @@ pub trait VfsOps {
/// Gets the file system root vnode.
fn vfs_root(&self) -> Result<Arc<Spinlock<VNode>>>;

/// Queries or modifies space quotas.
fn vfs_quotactl(&self) {
unimplemented!("{} does not implement vfs_quotactl", self.vfs_name());
}

/// Gets file system statistics.
fn vfs_statvfs(&self) {
unimplemented!("{} does not implement vfs_statvfs", self.vfs_name());
todo!("{} does not implement vfs_statvfs", self.vfs_name());
}

/// Flushes file system buffers.
Expand All @@ -118,37 +97,20 @@ pub trait VfsOps {

fn vfs_lookup(&self, path: &PathBuf) -> Result<Arc<Spinlock<VNode>>>;

/// Converts a NFS file handle to a vnode.
fn vfs_fhtovp(&self) {
unimplemented!("{} does not implement vfs_fhtovp", self.vfs_name());
}

/// Converts a vnode to a NFS file handle.
fn vfs_vptofh(&self) {
unimplemented!("{} does not implement vfs_vptofh", self.vfs_name());
}

/// Initializes the file system driver.
fn vfs_init(&mut self);

/// Reinitializes the file system driver.
fn vfs_reinit(&self) {
unimplemented!("{} does not implement vfs_reinit", self.vfs_name());
todo!("{} does not implement vfs_reinit", self.vfs_name());
}

/// Finalizes the file system driver.
fn vfs_done(&self);

/// Mounts an instance of the file system as the root file system.
fn vfs_mountroot(&self) {
unimplemented!("{} does not implement vfs_mountroot", self.vfs_name());
}

/// Controls extended attributes.
// The generic vfs_stdextattrctl function is provided as a simple hook for file system that do not support this operation
// TODO: create a generic vfs_stdextattrctl function
fn vfs_extattrctl(&self) {
unimplemented!("{} does not implement vfs_extattrctl", self.vfs_name());
todo!("{} does not implement vfs_mountroot", self.vfs_name());
}

/// Returns the name of the file system
Expand Down
8 changes: 6 additions & 2 deletions kernel/src/fs/pathbuf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,14 @@ impl PathBuf {
}

// TODO:
pub fn push(&mut self) {}
pub fn push(&mut self) {
todo!();
}

// TODO:
pub fn pop(&mut self) {}
pub fn pop(&mut self) {
todo!();
}
}

impl From<String> for PathBuf {
Expand Down
14 changes: 7 additions & 7 deletions kernel/src/fs/tmpfs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use alloc::{
};
use libxernel::{boot::InitAtBoot, sync::Spinlock};

use crate::{fs::Error, fs::Result};
use crate::{fs::Result, fs::VfsError};

use super::{
mount::{Mount, VfsOps},
Expand Down Expand Up @@ -137,7 +137,7 @@ impl VNodeOperations for TmpfsNode {
if let TmpfsNodeData::Directory(children) = &mut self.data {
children.push((PathBuf::from(file_name), new_node.clone()));
} else {
return Err(Error::NotADirectory);
return Err(VfsError::NotADirectory);
}

Ok(new_node)
Expand Down Expand Up @@ -165,7 +165,7 @@ impl VNodeOperations for TmpfsNode {
.iter()
.find(|(pt, _)| pt == components[0])
.map(|(_, node)| node.clone());
node.ok_or(Error::EntryNotFound)
node.ok_or(VfsError::EntryNotFound)
}
core::cmp::Ordering::Greater => {
let node = children
Expand All @@ -176,13 +176,13 @@ impl VNodeOperations for TmpfsNode {
if let Some(node) = node {
return node.lock().lookup(&stripped_path);
} else {
Err(Error::EntryNotFound)
Err(VfsError::EntryNotFound)
}
}
core::cmp::Ordering::Less => todo!(),
}
} else {
Err(Error::NotADirectory)
Err(VfsError::NotADirectory)
}
}

Expand All @@ -202,7 +202,7 @@ impl VNodeOperations for TmpfsNode {

Ok(max_read)
} else {
Err(Error::IsADirectory)
Err(VfsError::IsADirectory)
}
}

Expand All @@ -218,7 +218,7 @@ impl VNodeOperations for TmpfsNode {

Ok(max_write)
} else {
Err(Error::IsADirectory)
Err(VfsError::IsADirectory)
}
}

Expand Down
12 changes: 6 additions & 6 deletions kernel/src/fs/vfs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use super::{
pathbuf::PathBuf,
tmpfs::Tmpfs,
vnode::VNode,
{Error, Result},
{Result, VfsError},
};

pub static VFS: Spinlock<Vfs> = Spinlock::new(Vfs::new());
Expand Down Expand Up @@ -45,7 +45,7 @@ impl Vfs {
.iter()
.find(|(pt, _)| pt == mounted_on)
.map(|(_, mnt)| mnt)
.ok_or(Error::MountPointNotFound)
.ok_or(VfsError::MountPointNotFound)
.cloned()
}

Expand All @@ -59,7 +59,7 @@ impl Vfs {
.iter()
.find(|(name, _)| name == name_of_fs)
.map(|(_, driver)| driver)
.ok_or(Error::FileSystemNotFound)?;
.ok_or(VfsError::FileSystemNotFound)?;

let node_covered = if where_to_mount == "/" {
None
Expand All @@ -68,7 +68,7 @@ impl Vfs {
if let Ok(node) = self.lookuppn(where_to_mount.to_string()) {
Some(node)
} else {
return Err(Error::EntryNotFound);
return Err(VfsError::EntryNotFound);
}
};

Expand Down Expand Up @@ -98,7 +98,7 @@ impl Vfs {
.iter()
.find(|(pt, _)| pt == mnt_point)
.map(|(_, mnt)| mnt)
.ok_or(Error::MountPointNotFound)?;
.ok_or(VfsError::MountPointNotFound)?;

mnt.lock().vfs_lookup(&path.strip_prefix(mnt_point))
}
Expand All @@ -110,7 +110,7 @@ impl Vfs {
.filter(|(pt, _)| path.starts_with(pt))
.max_by_key(|(pt, _)| pt.len())
.map(|(pt, _)| pt)
.ok_or(Error::MountPointNotFound)?;
.ok_or(VfsError::MountPointNotFound)?;

Ok(mnt_point)
}
Expand Down
Loading

0 comments on commit 39ca2e5

Please sign in to comment.