1
0
Fork 0
forked from koniifer/ableos

VFS+Ext2: many simplifications and filesystem initialisation

This commit is contained in:
TheOddGarlic 2022-08-04 17:03:44 +03:00
parent 52cd72a949
commit e766229b58
8 changed files with 116 additions and 173 deletions

View file

@ -4,6 +4,7 @@
* SPDX-License-Identifier: MPL-2.0 * SPDX-License-Identifier: MPL-2.0
*/ */
#[derive(Copy, Clone, Debug)]
pub enum FsError { pub enum FsError {
EndOfFile, EndOfFile,
InodeNotFound, InodeNotFound,

View file

@ -4,16 +4,17 @@
* SPDX-License-Identifier: MPL-2.0 * SPDX-License-Identifier: MPL-2.0
*/ */
use alloc::sync::Weak; use alloc::sync::{Arc, Weak};
use ext2::fs::{sync::Synced, Ext2}; use ext2::fs::{sync::Synced, Ext2};
use ext2::sector::SectorSize; use ext2::sector::SectorSize;
use ext2::sys::inode::TypePerm; use ext2::sys::inode::TypePerm;
use ext2::volume::Volume; use ext2::volume::Volume;
use crate::handle::Handle; use crate::handle::{Handle, HandleResource};
use super::errors::FsError; use super::errors::FsError;
use super::{DirectoryEntry, FsFlags, FsNode, FsResult as Result, StorageDevice}; use super::vfs::{add_fs_node, find_fs_node, DirectoryEntry, FsFlags};
use super::{FsNode, FsResult as Result, StorageDevice};
pub struct Ext2StorageDevice<S, V> pub struct Ext2StorageDevice<S, V>
where where
@ -22,6 +23,7 @@ where
{ {
fs: Synced<Ext2<S, V>>, fs: Synced<Ext2<S, V>>,
device_handle: Handle, device_handle: Handle,
root: Arc<FsNode>,
} }
impl<S, V> Ext2StorageDevice<S, V> impl<S, V> Ext2StorageDevice<S, V>
@ -29,10 +31,24 @@ where
S: SectorSize, S: SectorSize,
V: Volume<u8, S>, V: Volume<u8, S>,
{ {
pub fn new(volume: V, device_handle: Handle) -> Result<Self> { pub fn new(volume: V) -> Result<Self> {
Ok(Self { let fs = Synced::new(volume).map_err(|e| e.into())?;
fs: Synced::new(volume).map_err(|e| e.into())?, let root_inode = fs.root_inode();
let device_handle = Handle::new(HandleResource::StorageDevice);
let root = Arc::new(FsNode::new(
FsFlags::DIRECTORY,
root_inode.size(),
2,
device_handle, device_handle,
Weak::new(),
));
add_fs_node(root.clone());
Ok(Self {
fs,
device_handle,
root,
}) })
} }
} }
@ -45,7 +61,7 @@ where
fn read(&self, node: &FsNode, offset: usize, size: usize, buffer: &mut Vec<u8>) -> Result<()> { fn read(&self, node: &FsNode, offset: usize, size: usize, buffer: &mut Vec<u8>) -> Result<()> {
let inode = self let inode = self
.fs .fs
.inode_nth(node.inode as usize) .inode_nth(node.inode() as usize)
.ok_or_else(|| FsError::InodeNotFound)?; .ok_or_else(|| FsError::InodeNotFound)?;
// FIXME: I don't really know how Ext2 works, so for now we don't // FIXME: I don't really know how Ext2 works, so for now we don't
@ -66,21 +82,38 @@ where
fn read_dir(&self, node: &FsNode, index: usize) -> Result<DirectoryEntry> { fn read_dir(&self, node: &FsNode, index: usize) -> Result<DirectoryEntry> {
let inode = self let inode = self
.fs .fs
.inode_nth(node.inode as usize) .inode_nth(node.inode() as usize)
.ok_or_else(|| FsError::InodeNotFound)?; .ok_or_else(|| FsError::InodeNotFound)?;
let mut dir = inode.directory().ok_or_else(|| FsError::NotADirectory)?; let mut dir = inode.directory().ok_or_else(|| FsError::NotADirectory)?;
let entry = dir let entry = dir
.nth(index) .nth(index)
.ok_or_else(|| FsError::InodeNotFound)? .ok_or_else(|| FsError::InodeNotFound)?
.map_err(|e| e.into())?; .map_err(|e| e.into())?;
let entry_inode = self
.fs
.inode_nth(entry.inode)
.ok_or_else(|| FsError::InodeNotFound)?;
let entry_node_handle =
find_fs_node(entry.inode, self.device_handle).unwrap_or_else(|| {
add_fs_node(Arc::new(FsNode::new(
ext2_type_to_fs_flags(entry_inode.type_perm()),
inode.size(),
entry.inode,
self.device_handle,
Weak::new(),
)))
});
Ok(DirectoryEntry::new(entry.file_name_string(), entry.inode)) Ok(DirectoryEntry::new(
entry.file_name_string(),
entry_node_handle,
))
} }
fn find_dir(&self, node: &FsNode, name: &str) -> Result<FsNode> { fn find_dir(&self, node: &FsNode, name: &str) -> Result<Handle> {
let inode = self let inode = self
.fs .fs
.inode_nth(node.inode as usize) .inode_nth(node.inode() as usize)
.ok_or_else(|| FsError::InodeNotFound)?; .ok_or_else(|| FsError::InodeNotFound)?;
let dir = inode.directory().ok_or_else(|| FsError::NotADirectory)?; let dir = inode.directory().ok_or_else(|| FsError::NotADirectory)?;
let mut found_node = Err(FsError::NotFound); let mut found_node = Err(FsError::NotFound);
@ -91,23 +124,30 @@ where
let entry = entry.unwrap(); let entry = entry.unwrap();
if entry.file_name_string() == name { if entry.file_name_string() == name {
let inode = self found_node = Ok(
.fs find_fs_node(entry.inode, self.device_handle).unwrap_or_else(|| {
.inode_nth(entry.inode as usize) add_fs_node(Arc::new(FsNode::new(
.ok_or_else(|| FsError::InodeNotFound)?; ext2_type_to_fs_flags(inode.type_perm()),
let flags = ext2_type_to_fs_flags(inode.type_perm()); inode.size(),
found_node = Ok(FsNode::new( entry.inode,
flags, self.device_handle,
inode.size(), Weak::new(),
entry.inode, )))
self.device_handle, }),
Weak::new(), );
))
} }
} }
found_node found_node
} }
fn root_node(&self) -> Arc<FsNode> {
self.root.clone()
}
fn device_handle(&self) -> Handle {
self.device_handle
}
} }
fn ext2_type_to_fs_flags(type_perm: TypePerm) -> FsFlags { fn ext2_type_to_fs_flags(type_perm: TypePerm) -> FsFlags {
@ -127,13 +167,6 @@ fn ext2_type_to_fs_flags(type_perm: TypePerm) -> FsFlags {
t | s t | s
} }
// fn load_fs() -> Arc<Mutex<Ext2<Size1024, Vec<u8>>>> {
// let mut volume = Vec::new();
// volume.extend_from_slice(include_bytes!("../../../userland/root_fs/ext2.img"));
// Arc::<Ext2<Size1024, _>>::new(volume).unwrap()
// }
// pub fn walk<S: SectorSize, V: Volume<u8, S>>( // pub fn walk<S: SectorSize, V: Volume<u8, S>>(
// fs: &Synced<Ext2<S, V>>, // fs: &Synced<Ext2<S, V>>,
// inode: Inode<S, V>, // inode: Inode<S, V>,

View file

@ -6,15 +6,18 @@
pub mod errors; pub mod errors;
pub mod ext2; pub mod ext2;
pub mod vfs;
use core::cmp; use ::ext2::sector::Size1024;
use alloc::sync::Arc;
use alloc::sync::{Arc, Weak};
use bitflags::bitflags;
use crate::{handle::Handle, KERNEL_STATE}; use crate::{handle::Handle, KERNEL_STATE};
use self::errors::FsError; use self::{
errors::FsError,
ext2::Ext2StorageDevice,
vfs::{DirectoryEntry, FsNode},
};
use FsResult as Result; use FsResult as Result;
pub type FsResult<T> = core::result::Result<T, FsError>; pub type FsResult<T> = core::result::Result<T, FsError>;
@ -27,138 +30,22 @@ where
fn read(&self, node: &FsNode, offset: usize, size: usize, buffer: &mut Vec<u8>) -> Result<()>; fn read(&self, node: &FsNode, offset: usize, size: usize, buffer: &mut Vec<u8>) -> Result<()>;
fn write(&self, node: &FsNode, offset: usize, buffer: &[u8]) -> Result<()>; fn write(&self, node: &FsNode, offset: usize, buffer: &[u8]) -> Result<()>;
fn read_dir(&self, node: &FsNode, index: usize) -> Result<DirectoryEntry>; fn read_dir(&self, node: &FsNode, index: usize) -> Result<DirectoryEntry>;
fn find_dir(&self, node: &FsNode, name: &str) -> Result<FsNode>; fn find_dir(&self, node: &FsNode, name: &str) -> Result<Handle>;
// TODO: flush to disk // TODO: flush to disk
fn root_node(&self) -> Arc<FsNode>;
fn device_handle(&self) -> Handle;
} }
/// A VFS node, that can either be a file or a directory. pub fn init() -> Result<()> {
/// let mut state = KERNEL_STATE.lock();
/// VFS nodes are created whenever a file that doesn't have an open VFS node is state.add_storage_device(load_fs()?);
/// opened. When there are no open file descriptors to a file, the associated Ok(())
/// VFS node is dropped.
#[derive(Debug)]
pub struct FsNode {
flags: FsFlags,
length: usize, // in bytes
inode: usize, // implementation specific identifier for the node
device_handle: Handle, // uniquely assigned device handle
ptr: Weak<FsNode>, // used by mountpoints and symlinks
// TODO: permissions mask
// TODO: owning user/group
} }
impl FsNode { fn load_fs() -> Result<Ext2StorageDevice<Size1024, Vec<u8>>> {
pub fn new( let mut volume = Vec::new();
flags: FsFlags, volume.extend_from_slice(include_bytes!("../../../userland/root_fs/ext2.img"));
length: usize,
inode: usize,
device_handle: Handle,
ptr: Weak<FsNode>,
) -> Self {
Self {
flags,
length,
inode,
device_handle,
ptr,
}
}
/// This method opens a new file descriptor for this VFS node. Ext2StorageDevice::new(volume)
// TODO: make this take flags
pub fn open(self: Arc<Self>) -> Result<Handle> {
let mut state = KERNEL_STATE.lock();
let handle = state.open_file_descriptor(self);
Ok(handle)
}
/// This method reads from the VFS node without opening a new file
/// descriptor. This is intended to be used internally, if you're trying to
/// read a file then you probably want to read from a file descriptor.
pub fn read(&self, offset: usize, size: usize, buffer: &mut Vec<u8>) -> Result<usize> {
let state = KERNEL_STATE.lock();
let device = state
.get_storage_device(self.device_handle)
.ok_or_else(|| FsError::InvalidDevice)?;
if self.is_dir() {
Err(FsError::IsDirectory)?;
}
if offset > self.length {
Err(FsError::EndOfFile)?;
}
let readable_size = cmp::min(size, self.length - offset);
device.read(self, offset, readable_size, buffer)?;
Ok(readable_size)
}
/// This method writes to the VFS node without opening a new file
/// descriptor. This is intended to be used internally, if you're trying to
/// write to a file then you probably want to write to a file descriptor.
pub fn write(&self, offset: usize, buffer: &[u8]) -> Result<()> {
let state = KERNEL_STATE.lock();
let device = state
.get_storage_device(self.device_handle)
.ok_or_else(|| FsError::InvalidDevice)?;
device.write(self, offset, buffer)
}
pub fn read_dir(&self, index: usize) -> Result<DirectoryEntry> {
let state = KERNEL_STATE.lock();
let device = state
.get_storage_device(self.device_handle)
.ok_or_else(|| FsError::InvalidDevice)?;
device.read_dir(self, index)
}
pub fn find_dir(&self, name: &str) -> Result<FsNode> {
let state = KERNEL_STATE.lock();
let device = state
.get_storage_device(self.device_handle)
.ok_or_else(|| FsError::InvalidDevice)?;
device.find_dir(self, name)
}
pub fn is_dir(&self) -> bool {
(self.flags & FsFlags::DIRECTORY) == FsFlags::DIRECTORY
}
}
impl Drop for FsNode {
fn drop(&mut self) {
trace!("dropping VFS node: {self:#?}");
// TODO: flush to disk here
}
}
bitflags! {
/// Flags associated with VFS nodes and file descriptors
///
/// 0x00000MST \
/// T is set to 0 for files, 1 for directories \
/// S is set when the file is a symbolic link \
/// M is set if the file is an active mount point
pub struct FsFlags: u8 {
const FILE = 0b00000000;
const DIRECTORY = 0b00000001;
const SYMBOLIC_LINK = 0b00000010;
const MOUNT_POINT = 0b00000100;
}
}
pub struct DirectoryEntry {
name: String,
inode: usize,
}
impl DirectoryEntry {
fn new(name: String, inode: usize) -> Self {
Self { name, inode }
}
} }

View file

@ -23,6 +23,7 @@ pub enum HandleResource {
BinaryData, BinaryData,
StorageDevice, StorageDevice,
FileDescriptor, FileDescriptor,
FsNode,
} }
#[derive(Debug, Eq, Hash, PartialEq, Clone, Copy)] #[derive(Debug, Eq, Hash, PartialEq, Clone, Copy)]
@ -40,6 +41,7 @@ impl Display for Handle {
HandleResource::Socket => write!(f, "-Socket")?, HandleResource::Socket => write!(f, "-Socket")?,
HandleResource::StorageDevice => write!(f, "-StorageDevice")?, HandleResource::StorageDevice => write!(f, "-StorageDevice")?,
HandleResource::FileDescriptor => write!(f, "-FileDescriptor")?, HandleResource::FileDescriptor => write!(f, "-FileDescriptor")?,
HandleResource::FsNode => write!(f, "-FsNode")?,
} }
Ok(()) Ok(())

View file

@ -1,9 +1,15 @@
/*
* Copyright (c) 2022, Umut İnan Erdoğan <umutinanerdogan@pm.me>
*
* SPDX-License-Identifier: MPL-2.0
*/
use alloc::sync::Arc; use alloc::sync::Arc;
use hashbrown::HashMap; use hashbrown::HashMap;
use spin::Lazy; use spin::Lazy;
use crate::{ use crate::{
filesystem::{FsNode, StorageDevice}, filesystem::{vfs::FsNode, StorageDevice},
handle::{Handle, HandleResource}, handle::{Handle, HandleResource},
}; };
@ -32,13 +38,12 @@ impl KernelInternalState {
self.hostname = hostname; self.hostname = hostname;
} }
pub fn add_storage_device(&mut self, device: impl StorageDevice + Send + 'static) -> Handle { pub fn add_storage_device(&mut self, device: impl StorageDevice + Send + 'static) {
let handle = Handle::new(HandleResource::StorageDevice); let device = Box::new(device);
self.storage_devices.insert(handle, Box::new(device)); self.storage_devices.insert(device.device_handle(), device);
handle
} }
pub fn get_storage_device(&self, handle: Handle) -> Option<&dyn StorageDevice> { pub fn storage_device(&self, handle: Handle) -> Option<&dyn StorageDevice> {
self.storage_devices.get(&handle).map(|d| &**d) self.storage_devices.get(&handle).map(|d| &**d)
} }
@ -49,7 +54,7 @@ impl KernelInternalState {
handle handle
} }
pub fn get_file_descriptor(&self, handle: Handle) -> Option<&FileTableEntry> { pub fn file_descriptor(&self, handle: Handle) -> Option<&FileTableEntry> {
self.file_table.get(&handle) self.file_table.get(&handle)
} }
@ -83,4 +88,8 @@ impl FileTableEntry {
fn new(fs_node: Arc<FsNode>) -> Self { fn new(fs_node: Arc<FsNode>) -> Self {
Self { fs_node } Self { fs_node }
} }
pub fn fs_node(&self) -> Arc<FsNode> {
self.fs_node.clone()
}
} }

View file

@ -1,13 +1,19 @@
/*
* Copyright (c) 2022, Umut İnan Erdoğan <umutinanerdogan@pm.me>
*
* SPDX-License-Identifier: MPL-2.0
*/
#![allow(clippy::empty_loop)] #![allow(clippy::empty_loop)]
use core::sync::atomic::AtomicU64; use core::sync::atomic::AtomicU64;
use crate::arch::{drivers::sysinfo::master, init, sloop}; use crate::arch::{drivers::sysinfo::master, init, sloop};
use crate::hardware;
use crate::relib::network::socket::{SimpleSock, Socket}; use crate::relib::network::socket::{SimpleSock, Socket};
use crate::{ use crate::{
boot_conf::KernelConfig, scratchpad, systeminfo::RELEASE_TYPE, TERM, boot_conf::KernelConfig, scratchpad, systeminfo::RELEASE_TYPE, TERM,
}; };
use crate::{filesystem, hardware};
use kernel::KERNEL_VERSION; use kernel::KERNEL_VERSION;
use spin::Lazy; use spin::Lazy;
@ -36,6 +42,9 @@ pub fn kernel_main() -> ! {
x86_64::instructions::interrupts::without_interrupts(|| { x86_64::instructions::interrupts::without_interrupts(|| {
hardware::init_mouse(); hardware::init_mouse();
}); });
filesystem::init().unwrap();
/* /*
// println!("abc"); // println!("abc");

View file

@ -1,5 +1,6 @@
/* /*
* Copyright (c) 2022, Able <able@ablecorp.us> * Copyright (c) 2022, Able <able@ablecorp.us>
* Copyright (c) 2022, Umut İnan Erdoğan <umutinanerdogan@pm.me>
* *
* SPDX-License-Identifier: MPL-2.0 * SPDX-License-Identifier: MPL-2.0
*/ */
@ -25,6 +26,9 @@
#[macro_use] #[macro_use]
pub extern crate log; pub extern crate log;
#[macro_use]
pub extern crate bitflags;
pub extern crate alloc; pub extern crate alloc;
pub extern crate externc_libm as libm; pub extern crate externc_libm as libm;

View file

@ -393,8 +393,6 @@ impl<S: SectorSize, V: Volume<u8, S>> Inode<S, V> {
} }
/// Determine if an inode is a directory /// Determine if an inode is a directory
pub fn is_dir(&self) -> bool { pub fn is_dir(&self) -> bool {
use sys::inode::TypePerm;
{ self.inner.type_perm }.contains(TypePerm::DIRECTORY) { self.inner.type_perm }.contains(TypePerm::DIRECTORY)
// self.inner.type_perm.contains(TypePerm::DIRECTORY) // self.inner.type_perm.contains(TypePerm::DIRECTORY)
} }