vfs: FsNode::release() implementation and weak ref to VFS node in file descriptors
This commit is contained in:
parent
2a163a55ee
commit
600a81adf7
|
@ -4,12 +4,13 @@
|
||||||
* SPDX-License-Identifier: MPL-2.0
|
* SPDX-License-Identifier: MPL-2.0
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
use alloc::sync::Arc;
|
||||||
use ext2::fs::{sync::Synced, 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::errors::FsError;
|
||||||
use super::{FsResult as Result, StorageDevice, DirectoryEntry, FsNode, FileDescriptor};
|
use super::{DirectoryEntry, FileDescriptor, FsNode, FsResult as Result, StorageDevice};
|
||||||
|
|
||||||
pub struct Ext2StorageDevice<S, V>
|
pub struct Ext2StorageDevice<S, V>
|
||||||
where
|
where
|
||||||
|
@ -36,32 +37,33 @@ where
|
||||||
S: SectorSize + Send,
|
S: SectorSize + Send,
|
||||||
V: Volume<u8, S> + Send,
|
V: Volume<u8, S> + Send,
|
||||||
{
|
{
|
||||||
fn open(&self, node: &super::FsNode /* TODO: flags */) -> Result<FileDescriptor> {
|
fn open(&self, node: Arc<FsNode> /* TODO: flags */) -> Result<FileDescriptor> {
|
||||||
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)?;
|
||||||
|
|
||||||
Ok(FileDescriptor::new(node.flags, inode.size(), node.inode))
|
Ok(FileDescriptor::new(Arc::downgrade(&node), node.flags, inode.size(), node.inode))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn release(&self, node: &super::FsNode) -> Result<()> {
|
fn release(&self, _node: FsNode) -> Result<()> {
|
||||||
|
// TODO: flush to disk and whatnot
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn read(&self, _node: &FsNode, _offset: usize, _size: usize) -> Result<Box<[u8]>> {
|
||||||
todo!()
|
todo!()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn read(&self, node: &super::FsNode, offset: usize, size: usize) -> Result<Box<[u8]>> {
|
fn write(&self, _node: &FsNode, _offset: usize, _buffer: Box<[u8]>) -> Result<()> {
|
||||||
todo!()
|
todo!()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn write(&self, node: &super::FsNode, offset: usize, buffer: Box<[u8]>) -> Result<()> {
|
fn read_dir(&self, _node: &FsNode, _index: usize) -> Result<DirectoryEntry> {
|
||||||
todo!()
|
todo!()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn read_dir(&self, node: &super::FsNode, index: usize) -> Result<DirectoryEntry> {
|
fn find_dir(&self, _node: &FsNode, _name: &str) -> Result<FsNode> {
|
||||||
todo!()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn find_dir(&self, node: &super::FsNode, name: &str) -> Result<FsNode> {
|
|
||||||
todo!()
|
todo!()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,7 +7,7 @@
|
||||||
pub mod errors;
|
pub mod errors;
|
||||||
pub mod ext2;
|
pub mod ext2;
|
||||||
|
|
||||||
use alloc::rc::Weak;
|
use alloc::sync::{Weak, Arc};
|
||||||
use bitflags::bitflags;
|
use bitflags::bitflags;
|
||||||
|
|
||||||
use crate::{handle::Handle, KERNEL_STATE};
|
use crate::{handle::Handle, KERNEL_STATE};
|
||||||
|
@ -21,8 +21,8 @@ pub trait StorageDevice
|
||||||
where
|
where
|
||||||
Self: Send,
|
Self: Send,
|
||||||
{
|
{
|
||||||
fn open(&self, node: &FsNode /* TODO: flags */) -> Result<FileDescriptor>;
|
fn open(&self, node: Arc<FsNode> /* TODO: flags */) -> Result<FileDescriptor>;
|
||||||
fn release(&self, node: &FsNode) -> Result<()>;
|
fn release(&self, node: FsNode) -> Result<()>;
|
||||||
fn read(&self, node: &FsNode, offset: usize, size: usize) -> Result<Box<[u8]>>;
|
fn read(&self, node: &FsNode, offset: usize, size: usize) -> Result<Box<[u8]>>;
|
||||||
fn write(&self, node: &FsNode, offset: usize, buffer: Box<[u8]>) -> Result<()>;
|
fn write(&self, node: &FsNode, offset: usize, buffer: Box<[u8]>) -> Result<()>;
|
||||||
fn read_dir(&self, node: &FsNode, index: usize) -> Result<DirectoryEntry>;
|
fn read_dir(&self, node: &FsNode, index: usize) -> Result<DirectoryEntry>;
|
||||||
|
@ -34,10 +34,10 @@ where
|
||||||
/// VFS nodes are created whenever a file that doesn't have an open VFS node is
|
/// 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
|
/// opened. When there are no open file descriptors to a file, the associated
|
||||||
/// VFS node is dropped.
|
/// VFS node is dropped.
|
||||||
|
#[derive(Debug)]
|
||||||
pub struct FsNode {
|
pub struct FsNode {
|
||||||
flags: FsFlags,
|
flags: FsFlags,
|
||||||
length: usize, // in bytes
|
length: usize, // in bytes
|
||||||
fd_count: usize, // count of open file descriptors
|
|
||||||
inode: usize, // implementation specific identifier for the node
|
inode: usize, // 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
|
||||||
|
@ -56,7 +56,6 @@ impl FsNode {
|
||||||
Self {
|
Self {
|
||||||
flags,
|
flags,
|
||||||
length,
|
length,
|
||||||
fd_count: 0,
|
|
||||||
inode,
|
inode,
|
||||||
device_handle,
|
device_handle,
|
||||||
ptr,
|
ptr,
|
||||||
|
@ -65,27 +64,31 @@ impl FsNode {
|
||||||
|
|
||||||
/// This method opens a new file descriptor for this VFS node.
|
/// This method opens a new file descriptor for this VFS node.
|
||||||
// TODO: make this take flags
|
// TODO: make this take flags
|
||||||
pub fn open(&mut self) -> Result<Handle> {
|
pub fn open(self: Arc<Self>) -> Result<Handle> {
|
||||||
let state = KERNEL_STATE.lock();
|
let mut state = KERNEL_STATE.lock();
|
||||||
let device = state
|
let device = state
|
||||||
.get_storage_device(self.device_handle)
|
.get_storage_device(self.device_handle)
|
||||||
.ok_or_else(|| FsError::InvalidDevice)?;
|
.ok_or_else(|| FsError::InvalidDevice)?;
|
||||||
let fd = device.open(self)?;
|
let fd = device.open(self)?;
|
||||||
let mut kernel_state = KERNEL_STATE.lock();
|
let handle = state.open_file_descriptor(fd);
|
||||||
let handle = kernel_state.add_file_descriptor(fd);
|
|
||||||
|
|
||||||
Ok(handle)
|
Ok(handle)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// This method is for closing the VFS node, which is done when no open file
|
/// This method is for closing the VFS node, which is usually done when no
|
||||||
/// descriptors for this file are left.
|
/// open file descriptors for this file are left. File descriptors have
|
||||||
pub fn release(&self) -> Result<()> {
|
/// weak references to the VFS node, meaning it is okay to close the VFS
|
||||||
|
/// node before all file descriptors are closed.
|
||||||
|
pub fn release(self) -> Result<()> {
|
||||||
let state = KERNEL_STATE.lock();
|
let state = KERNEL_STATE.lock();
|
||||||
let device = state
|
let device = state
|
||||||
.get_storage_device(self.device_handle)
|
.get_storage_device(self.device_handle)
|
||||||
.ok_or_else(|| FsError::InvalidDevice)?;
|
.ok_or_else(|| FsError::InvalidDevice)?;
|
||||||
|
|
||||||
device.release(self)
|
device.release(self)?;
|
||||||
|
// self is moved into device.release, and at the end of it, self should
|
||||||
|
// be dropped.
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// This method reads from the VFS node without opening a new file
|
/// This method reads from the VFS node without opening a new file
|
||||||
|
@ -131,6 +134,12 @@ impl FsNode {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Drop for FsNode {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
trace!("dropping: {self:#?}")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
bitflags! {
|
bitflags! {
|
||||||
/// Flags associated with VFS nodes and file descriptors
|
/// Flags associated with VFS nodes and file descriptors
|
||||||
///
|
///
|
||||||
|
@ -147,25 +156,38 @@ bitflags! {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A file descriptor.
|
/// A file descriptor.
|
||||||
|
#[derive(Debug)]
|
||||||
pub struct FileDescriptor {
|
pub struct FileDescriptor {
|
||||||
|
vfs_node: Weak<FsNode>, // ptr to the associated VFS node
|
||||||
flags: FsFlags,
|
flags: FsFlags,
|
||||||
length: usize, // in bytes
|
length: usize, // in bytes
|
||||||
inode: usize, // implementation specific identifier for the node
|
inode: usize, // implementation specific identifier for the node
|
||||||
// TODO: permissions mask
|
// TODO: permissions mask
|
||||||
// TODO: owning user/group
|
// TODO: owning user/group
|
||||||
// TODO: read file position?
|
// 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 {
|
impl FileDescriptor {
|
||||||
fn new(flags: FsFlags, length: usize, inode: usize) -> Self {
|
fn new(vfs_node: Weak<FsNode>, flags: FsFlags, length: usize, inode: usize) -> Self {
|
||||||
Self {
|
Self {
|
||||||
|
vfs_node,
|
||||||
flags,
|
flags,
|
||||||
length,
|
length,
|
||||||
inode,
|
inode,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// This method is for closing the file descriptor.
|
||||||
|
pub fn close(fd_handle: Handle) {
|
||||||
|
let mut state = KERNEL_STATE.lock();
|
||||||
|
state.close_file_descriptor(fd_handle);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Drop for FileDescriptor {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
trace!("dropping: {self:#?}")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct DirectoryEntry {
|
pub struct DirectoryEntry {
|
||||||
|
|
|
@ -2,7 +2,7 @@ use hashbrown::HashMap;
|
||||||
use spin::Lazy;
|
use spin::Lazy;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
filesystem::{StorageDevice, FileDescriptor},
|
filesystem::{FileDescriptor, StorageDevice},
|
||||||
handle::{Handle, HandleResource},
|
handle::{Handle, HandleResource},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -40,12 +40,16 @@ impl KernelInternalState {
|
||||||
self.storage_devices.get(&handle).map(|d| &**d)
|
self.storage_devices.get(&handle).map(|d| &**d)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn add_file_descriptor(&mut self, fd: FileDescriptor) -> Handle {
|
pub fn open_file_descriptor(&mut self, fd: FileDescriptor) -> Handle {
|
||||||
let handle = Handle::new(HandleResource::FileDescriptor);
|
let handle = Handle::new(HandleResource::FileDescriptor);
|
||||||
self.fd_table.insert(handle, fd);
|
self.fd_table.insert(handle, fd);
|
||||||
handle
|
handle
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn close_file_descriptor(&mut self, fd_handle: Handle) {
|
||||||
|
self.fd_table.remove(&fd_handle);
|
||||||
|
}
|
||||||
|
|
||||||
pub fn shutdown(&mut self) {
|
pub fn shutdown(&mut self) {
|
||||||
self.should_shutdown = true;
|
self.should_shutdown = true;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue