1
0
Fork 0
forked from AbleOS/ableos

vfs: documentation and a few minor changes

This commit is contained in:
TheOddGarlic 2022-08-03 20:30:05 +03:00
parent 9ed5ffc521
commit aa3667714b
5 changed files with 60 additions and 45 deletions

View file

@ -5,8 +5,9 @@
*/ */
pub enum FsError { pub enum FsError {
UnsupportedOperation, InodeNotFound,
InvalidDevice, InvalidDevice,
UnsupportedOperation,
} }
impl Into<FsError> for ext2::error::Error { impl Into<FsError> for ext2::error::Error {
@ -15,9 +16,16 @@ impl Into<FsError> for ext2::error::Error {
ext2::error::Error::Other(_) => todo!(), ext2::error::Error::Other(_) => todo!(),
ext2::error::Error::BadMagic { magic: _ } => todo!(), ext2::error::Error::BadMagic { magic: _ } => todo!(),
ext2::error::Error::OutOfBounds { index: _ } => todo!(), ext2::error::Error::OutOfBounds { index: _ } => todo!(),
ext2::error::Error::AddressOutOfBounds { sector: _, offset: _, size: _ } => todo!(), ext2::error::Error::AddressOutOfBounds {
ext2::error::Error::BadBlockGroupCount { by_blocks: _, by_inodes: _ } => todo!(), sector: _,
ext2::error::Error::InodeNotFound { inode: _ } => todo!(), offset: _,
size: _,
} => todo!(),
ext2::error::Error::BadBlockGroupCount {
by_blocks: _,
by_inodes: _,
} => todo!(),
ext2::error::Error::InodeNotFound { inode: _ } => FsError::InodeNotFound,
ext2::error::Error::NotADirectory { inode: _, name: _ } => todo!(), ext2::error::Error::NotADirectory { inode: _, name: _ } => todo!(),
ext2::error::Error::NotAbsolute { name: _ } => todo!(), ext2::error::Error::NotAbsolute { name: _ } => todo!(),
ext2::error::Error::NotFound { name: _ } => todo!(), ext2::error::Error::NotFound { name: _ } => todo!(),

View file

@ -4,10 +4,11 @@
* SPDX-License-Identifier: MPL-2.0 * SPDX-License-Identifier: MPL-2.0
*/ */
use ext2::fs::Ext2; use ext2::fs::{sync::Synced, Ext2};
use ext2::sector::SectorSize; use ext2::sector::SectorSize;
use ext2::volume::Volume; use ext2::volume::Volume;
use super::errors::FsError;
use super::{FsResult as Result, StorageDevice}; use super::{FsResult as Result, StorageDevice};
pub struct Ext2StorageDevice<S, V> pub struct Ext2StorageDevice<S, V>
@ -15,7 +16,7 @@ where
S: SectorSize, S: SectorSize,
V: Volume<u8, S>, V: Volume<u8, S>,
{ {
fs: Ext2<S, V>, fs: Synced<Ext2<S, V>>,
} }
impl<S, V> Ext2StorageDevice<S, V> impl<S, V> Ext2StorageDevice<S, V>
@ -25,7 +26,7 @@ where
{ {
pub fn new(volume: V) -> Result<Self> { pub fn new(volume: V) -> Result<Self> {
Ok(Self { Ok(Self {
fs: Ext2::new(volume).map_err(|e| e.into())?, fs: Synced::new(volume).map_err(|e| e.into())?,
}) })
} }
} }
@ -36,6 +37,11 @@ where
V: Volume<u8, S> + Send, V: Volume<u8, S> + Send,
{ {
fn open(&self, node: &super::FsNode /* TODO: flags */) -> Result<crate::handle::Handle> { fn open(&self, node: &super::FsNode /* TODO: flags */) -> Result<crate::handle::Handle> {
let inode = self
.fs
.inode_nth(node.inode as usize)
.ok_or_else(|| FsError::InodeNotFound)?;
todo!() todo!()
} }

View file

@ -30,9 +30,14 @@ where
} }
/// A VFS node, that can either be a file or a directory. /// A VFS node, that can either be a file or a directory.
///
/// VFS nodes are created whenever a file that doesn't have an open VFS node is
/// opened. When there are no open file descriptors to a file, the associated
/// VFS node is dropped.
pub struct FsNode { pub struct FsNode {
flags: FsNodeFlags, flags: FsNodeFlags,
length: u32, // in bytes length: u32, // in bytes
fd_count: u32, // count of open file descriptors
inode: u32, // implementation specific identifier for the node inode: u32, // implementation specific identifier for the node
device_handle: Handle, // uniquely assigned device handle device_handle: Handle, // uniquely assigned device handle
ptr: Weak<FsNode>, // used by mountpoints and symlinks ptr: Weak<FsNode>, // used by mountpoints and symlinks
@ -51,12 +56,14 @@ impl FsNode {
Self { Self {
flags, flags,
length, length,
fd_count: 0,
inode, inode,
device_handle, device_handle,
ptr, ptr,
} }
} }
/// This method opens a new file descriptor for this VFS node.
// TODO: make this take flags // TODO: make this take flags
pub fn open(&self) -> Result<Handle> { pub fn open(&self) -> Result<Handle> {
let state = KERNEL_STATE.lock(); let state = KERNEL_STATE.lock();
@ -67,6 +74,8 @@ impl FsNode {
device.open(self) device.open(self)
} }
/// This method is for closing the VFS node, which is done when no open file
/// descriptors for this file are left.
pub fn close(&self) -> Result<()> { pub fn close(&self) -> Result<()> {
let state = KERNEL_STATE.lock(); let state = KERNEL_STATE.lock();
let device = state let device = state
@ -76,6 +85,9 @@ impl FsNode {
device.close(self) device.close(self)
} }
/// 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: u32, size: u32) -> Result<Box<[u8]>> { pub fn read(&self, offset: u32, size: u32) -> Result<Box<[u8]>> {
let state = KERNEL_STATE.lock(); let state = KERNEL_STATE.lock();
let device = state let device = state
@ -85,6 +97,9 @@ impl FsNode {
device.read(self, offset, size) device.read(self, offset, 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: u32, buffer: Box<[u8]>) -> Result<()> { pub fn write(&self, offset: u32, buffer: Box<[u8]>) -> Result<()> {
let state = KERNEL_STATE.lock(); let state = KERNEL_STATE.lock();
let device = state let device = state
@ -128,6 +143,9 @@ bitflags! {
} }
} }
/// A file descriptor.
pub struct FileDescriptor {}
pub struct DirectoryEntry { pub struct DirectoryEntry {
name: String, name: String,
inode: u32, inode: u32,

View file

@ -1,7 +1,10 @@
use hashbrown::HashMap; use hashbrown::HashMap;
use spin::Lazy; use spin::Lazy;
use crate::{handle::{Handle, HandleResource}, filesystem::StorageDevice}; use crate::{
filesystem::StorageDevice,
handle::{Handle, HandleResource},
};
pub static KERNEL_STATE: Lazy<spin::Mutex<KernelInternalState>> = pub static KERNEL_STATE: Lazy<spin::Mutex<KernelInternalState>> =
Lazy::new(|| spin::Mutex::new(KernelInternalState::new())); Lazy::new(|| spin::Mutex::new(KernelInternalState::new()));
@ -26,7 +29,8 @@ impl KernelInternalState {
} }
pub fn add_storage_device(&mut self, device: impl StorageDevice + Send + 'static) { pub fn add_storage_device(&mut self, device: impl StorageDevice + Send + 'static) {
self.storage_devices.insert(Handle::new(HandleResource::StorageDevice), Box::new(device)); self.storage_devices
.insert(Handle::new(HandleResource::StorageDevice), Box::new(device));
} }
pub fn get_storage_device(&self, handle: Handle) -> Option<&dyn StorageDevice> { pub fn get_storage_device(&self, handle: Handle) -> Option<&dyn StorageDevice> {

View file

@ -11,7 +11,9 @@ use crate::systeminfo::{KERNEL_VERSION, RELEASE_TYPE};
use crate::time::fetch_time; use crate::time::fetch_time;
use crate::KERNEL_STATE; use crate::KERNEL_STATE;
use crate::{ use crate::{
arch::shutdown, rhai_shell::KEYBUFF, vterm::Term, arch::shutdown,
rhai_shell::KEYBUFF,
vterm::Term,
// wasm_jumploader::run_program, // wasm_jumploader::run_program,
}; };
@ -231,19 +233,12 @@ pub fn command_parser(user: String, command: String) {
// } // }
// } // }
<<<<<<< HEAD // "echo" => match conf_args.1.arguments.get("p") {
"echo" => match conf_args.1.arguments.get("p") { // Some(path) => echo_file(path.to_string(), fs),
Some(path) => echo_file(path.to_string(), fs),
None => println!("No path provided"), // None => println!("No path provided"),
},
"test" => {}
=======
// "echo" => {
// echo_file(iter.next().unwrap().to_string(), fs);
// } // }
>>>>>>> 5149f26... vfs: move operations into trait StorageDevice, hold StorageDevices in KERNEL_STATE "test" => {}
"quit" => shutdown(), "quit" => shutdown(),
_ => { _ => {
@ -265,21 +260,6 @@ pub fn command_parser(user: String, command: String) {
// trace!("{:?}", error); // trace!("{:?}", error);
println!("No such binary: {}", bin_name); println!("No such binary: {}", bin_name);
error!("No such binary: {}", bin_name); error!("No such binary: {}", bin_name);
return;
<<<<<<< HEAD
}
}
}
}
};
let mut binary = vec![];
file.read_to_end(&mut binary).unwrap();
// let args = iter.collect::<Vec<&str>>();
// println!("{:?}", args);
run_program(&binary);
=======
// } // }
// } // }
// } // }
@ -292,7 +272,6 @@ pub fn command_parser(user: String, command: String) {
// let args = iter.collect::<Vec<&str>>(); // let args = iter.collect::<Vec<&str>>();
// println!("{:?}", args); // println!("{:?}", args);
// run_program(&binary); // run_program(&binary);
>>>>>>> 5149f26... vfs: move operations into trait StorageDevice, hold StorageDevices in KERNEL_STATE
} }
} }
} }