2022-08-03 04:53:02 -05:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2022, Umut İnan Erdoğan <umutinanerdogan@pm.me>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: MPL-2.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
pub mod errors;
|
2022-08-03 07:45:49 -05:00
|
|
|
pub mod ext2;
|
2022-08-03 04:53:02 -05:00
|
|
|
|
|
|
|
use alloc::rc::Weak;
|
|
|
|
use bitflags::bitflags;
|
2022-08-03 07:45:49 -05:00
|
|
|
|
|
|
|
use crate::{handle::Handle, KERNEL_STATE};
|
2022-08-03 04:53:02 -05:00
|
|
|
|
|
|
|
use self::errors::FsError;
|
2022-08-03 06:23:58 -05:00
|
|
|
use FsResult as Result;
|
2022-08-03 04:53:02 -05:00
|
|
|
|
2022-08-03 06:23:58 -05:00
|
|
|
pub type FsResult<T> = core::result::Result<T, FsError>;
|
|
|
|
|
2022-08-03 07:45:49 -05:00
|
|
|
pub trait StorageDevice
|
|
|
|
where
|
|
|
|
Self: Send,
|
|
|
|
{
|
2022-08-03 13:16:18 -05:00
|
|
|
fn open(&self, node: &FsNode /* TODO: flags */) -> Result<FileDescriptor>;
|
2022-08-03 13:17:28 -05:00
|
|
|
fn release(&self, node: &FsNode) -> Result<()>;
|
2022-08-03 13:16:18 -05:00
|
|
|
fn read(&self, node: &FsNode, offset: usize, size: usize) -> Result<Box<[u8]>>;
|
|
|
|
fn write(&self, node: &FsNode, offset: usize, buffer: Box<[u8]>) -> Result<()>;
|
|
|
|
fn read_dir(&self, node: &FsNode, index: usize) -> Result<DirectoryEntry>;
|
2022-08-03 07:45:49 -05:00
|
|
|
fn find_dir(&self, node: &FsNode, name: &str) -> Result<FsNode>;
|
|
|
|
}
|
2022-08-03 04:53:02 -05:00
|
|
|
|
|
|
|
/// A VFS node, that can either be a file or a directory.
|
2022-08-03 12:30:05 -05:00
|
|
|
///
|
|
|
|
/// 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.
|
2022-08-03 04:53:02 -05:00
|
|
|
pub struct FsNode {
|
2022-08-03 13:16:18 -05:00
|
|
|
flags: FsFlags,
|
|
|
|
length: usize, // in bytes
|
|
|
|
fd_count: usize, // count of open file descriptors
|
|
|
|
inode: usize, // implementation specific identifier for the node
|
2022-08-03 04:53:02 -05:00
|
|
|
device_handle: Handle, // uniquely assigned device handle
|
2022-08-03 12:30:05 -05:00
|
|
|
ptr: Weak<FsNode>, // used by mountpoints and symlinks
|
2022-08-03 13:16:18 -05:00
|
|
|
// TODO: permissions mask
|
|
|
|
// TODO: owning user/group
|
2022-08-03 04:53:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl FsNode {
|
2022-08-03 11:45:04 -05:00
|
|
|
pub fn new(
|
2022-08-03 13:16:18 -05:00
|
|
|
flags: FsFlags,
|
|
|
|
length: usize,
|
|
|
|
inode: usize,
|
2022-08-03 11:45:04 -05:00
|
|
|
device_handle: Handle,
|
|
|
|
ptr: Weak<FsNode>,
|
|
|
|
) -> Self {
|
|
|
|
Self {
|
|
|
|
flags,
|
|
|
|
length,
|
2022-08-03 12:30:05 -05:00
|
|
|
fd_count: 0,
|
2022-08-03 11:45:04 -05:00
|
|
|
inode,
|
|
|
|
device_handle,
|
|
|
|
ptr,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-03 12:30:05 -05:00
|
|
|
/// This method opens a new file descriptor for this VFS node.
|
2022-08-03 04:53:02 -05:00
|
|
|
// TODO: make this take flags
|
2022-08-03 13:16:18 -05:00
|
|
|
pub fn open(&mut self) -> Result<Handle> {
|
2022-08-03 07:45:49 -05:00
|
|
|
let state = KERNEL_STATE.lock();
|
|
|
|
let device = state
|
|
|
|
.get_storage_device(self.device_handle)
|
|
|
|
.ok_or_else(|| FsError::InvalidDevice)?;
|
2022-08-03 13:16:18 -05:00
|
|
|
let fd = device.open(self)?;
|
|
|
|
let mut kernel_state = KERNEL_STATE.lock();
|
|
|
|
let handle = kernel_state.add_file_descriptor(fd);
|
2022-08-03 07:45:49 -05:00
|
|
|
|
2022-08-03 13:16:18 -05:00
|
|
|
Ok(handle)
|
2022-08-03 04:53:02 -05:00
|
|
|
}
|
|
|
|
|
2022-08-03 12:30:05 -05:00
|
|
|
/// This method is for closing the VFS node, which is done when no open file
|
|
|
|
/// descriptors for this file are left.
|
2022-08-03 13:17:28 -05:00
|
|
|
pub fn release(&self) -> Result<()> {
|
2022-08-03 07:45:49 -05:00
|
|
|
let state = KERNEL_STATE.lock();
|
|
|
|
let device = state
|
|
|
|
.get_storage_device(self.device_handle)
|
|
|
|
.ok_or_else(|| FsError::InvalidDevice)?;
|
|
|
|
|
2022-08-03 13:17:28 -05:00
|
|
|
device.release(self)
|
2022-08-03 04:53:02 -05:00
|
|
|
}
|
|
|
|
|
2022-08-03 12:30:05 -05:00
|
|
|
/// 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.
|
2022-08-03 13:16:18 -05:00
|
|
|
pub fn read(&self, offset: usize, size: usize) -> Result<Box<[u8]>> {
|
2022-08-03 07:45:49 -05:00
|
|
|
let state = KERNEL_STATE.lock();
|
|
|
|
let device = state
|
|
|
|
.get_storage_device(self.device_handle)
|
|
|
|
.ok_or_else(|| FsError::InvalidDevice)?;
|
|
|
|
|
|
|
|
device.read(self, offset, size)
|
2022-08-03 04:53:02 -05:00
|
|
|
}
|
|
|
|
|
2022-08-03 12:30:05 -05:00
|
|
|
/// 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.
|
2022-08-03 13:16:18 -05:00
|
|
|
pub fn write(&self, offset: usize, buffer: Box<[u8]>) -> Result<()> {
|
2022-08-03 07:45:49 -05:00
|
|
|
let state = KERNEL_STATE.lock();
|
|
|
|
let device = state
|
|
|
|
.get_storage_device(self.device_handle)
|
|
|
|
.ok_or_else(|| FsError::InvalidDevice)?;
|
|
|
|
|
|
|
|
device.write(self, offset, buffer)
|
2022-08-03 06:23:58 -05:00
|
|
|
}
|
|
|
|
|
2022-08-03 13:16:18 -05:00
|
|
|
pub fn read_dir(&self, index: usize) -> Result<DirectoryEntry> {
|
2022-08-03 07:45:49 -05:00
|
|
|
let state = KERNEL_STATE.lock();
|
|
|
|
let device = state
|
|
|
|
.get_storage_device(self.device_handle)
|
|
|
|
.ok_or_else(|| FsError::InvalidDevice)?;
|
|
|
|
|
|
|
|
device.read_dir(self, index)
|
2022-08-03 06:23:58 -05:00
|
|
|
}
|
|
|
|
|
2022-08-03 11:45:04 -05:00
|
|
|
pub fn find_dir(&self, name: &str) -> Result<FsNode> {
|
2022-08-03 07:45:49 -05:00
|
|
|
let state = KERNEL_STATE.lock();
|
|
|
|
let device = state
|
|
|
|
.get_storage_device(self.device_handle)
|
|
|
|
.ok_or_else(|| FsError::InvalidDevice)?;
|
|
|
|
|
|
|
|
device.find_dir(self, name)
|
2022-08-03 04:53:02 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bitflags! {
|
2022-08-03 13:16:18 -05:00
|
|
|
/// Flags associated with VFS nodes and file descriptors
|
2022-08-03 12:30:05 -05:00
|
|
|
///
|
2022-08-03 04:53:02 -05:00
|
|
|
/// 0x00000MST
|
|
|
|
/// T is set to 0 for files, 1 for directories
|
2022-08-03 13:16:18 -05:00
|
|
|
/// 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 {
|
2022-08-03 04:53:02 -05:00
|
|
|
const FILE = 0b00000000;
|
|
|
|
const DIRECTORY = 0b00000001;
|
|
|
|
const SYMBOLIC_LINK = 0b00000010;
|
|
|
|
const MOUNT_POINT = 0b00000100;
|
|
|
|
}
|
2022-08-03 06:23:58 -05:00
|
|
|
}
|
|
|
|
|
2022-08-03 12:30:05 -05:00
|
|
|
/// A file descriptor.
|
2022-08-03 13:16:18 -05:00
|
|
|
pub struct FileDescriptor {
|
|
|
|
flags: FsFlags,
|
|
|
|
length: usize, // in bytes
|
|
|
|
inode: usize, // implementation specific identifier for the node
|
|
|
|
// TODO: permissions mask
|
|
|
|
// TODO: owning user/group
|
|
|
|
// TODO: read file position?
|
|
|
|
// FIXME: I'm not sure if this needs to have a ptr to the VFS node,
|
|
|
|
// figure that out.
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FileDescriptor {
|
|
|
|
fn new(flags: FsFlags, length: usize, inode: usize) -> Self {
|
|
|
|
Self {
|
|
|
|
flags,
|
|
|
|
length,
|
|
|
|
inode,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-08-03 12:30:05 -05:00
|
|
|
|
2022-08-03 06:23:58 -05:00
|
|
|
pub struct DirectoryEntry {
|
|
|
|
name: String,
|
2022-08-03 13:16:18 -05:00
|
|
|
inode: usize,
|
2022-08-03 04:53:02 -05:00
|
|
|
}
|