trick to add device inode

master
gaotianyu1350 6 years ago
parent 1cc5fd90bb
commit de6b914cd1

@ -12,6 +12,7 @@ use alloc::{
sync::{Arc, Weak},
vec::Vec,
vec,
boxed::Box
};
use core::any::Any;
use core::fmt::{Debug, Error, Formatter};
@ -63,6 +64,7 @@ pub struct INodeImpl {
disk_inode: RwLock<Dirty<DiskINode>>,
/// Weak reference to SFS, used by almost all operations
fs: Arc<SimpleFileSystem>,
device_inode: Option<Arc<DeviceINode>>
}
impl Debug for INodeImpl {
@ -379,24 +381,36 @@ impl INodeImpl {
impl vfs::INode for INodeImpl {
fn read_at(&self, offset: usize, buf: &mut [u8]) -> vfs::Result<usize> {
if self.disk_inode.read().type_ != FileType::File
&& self.disk_inode.read().type_ != FileType::SymLink
{
return Err(FsError::NotFile);
match self.disk_inode.read().type_ {
FileType::File => self._read_at(offset, buf),
FileType::SymLink => self._read_at(offset, buf),
FileType::CharDevice => {
match &self.device_inode {
Some(x) => x.read_at(offset, buf),
None => Err(FsError::NoDevice)
}
},
_ => Err(FsError::NotFile)
}
self._read_at(offset, buf)
}
fn write_at(&self, offset: usize, buf: &[u8]) -> vfs::Result<usize> {
let DiskINode { type_, size, .. } = **self.disk_inode.read();
if type_ != FileType::File && type_ != FileType::SymLink {
return Err(FsError::NotFile);
}
// resize if not large enough
let end_offset = offset + buf.len();
if (size as usize) < end_offset {
self._resize(end_offset)?;
match type_ {
FileType::File | FileType::SymLink => {
let end_offset = offset + buf.len();
if (size as usize) < end_offset {
self._resize(end_offset)?;
}
self._write_at(offset, buf)
},
FileType::CharDevice => {
match &self.device_inode {
Some(x) => x.write_at(offset, buf),
None => Err(FsError::NoDevice)
}
},
_ => Err(FsError::NotFile)
}
self._write_at(offset, buf)
}
/// the size returned here is logical size(entry num for directory), not the disk space used.
fn metadata(&self) -> vfs::Result<vfs::Metadata> {
@ -407,6 +421,8 @@ impl vfs::INode for INodeImpl {
size: match disk_inode.type_ {
FileType::File | FileType::SymLink => disk_inode.size as usize,
FileType::Dir => disk_inode.blocks as usize,
FileType::CharDevice => 0,
FileType::BlockDevice => 0,
_ => panic!("Unknown file type"),
},
mode: 0o777,
@ -786,6 +802,7 @@ impl SimpleFileSystem {
id,
disk_inode: RwLock::new(disk_inode),
fs: self.self_ptr.upgrade().unwrap(),
device_inode: None
});
self.inodes.write().insert(id, Arc::downgrade(&inode));
inode
@ -929,6 +946,8 @@ impl From<FileType> for vfs::FileType {
FileType::File => vfs::FileType::File,
FileType::SymLink => vfs::FileType::SymLink,
FileType::Dir => vfs::FileType::Dir,
FileType::CharDevice => vfs::FileType::CharDevice,
FileType::BlockDevice => vfs::FileType::BlockDevice,
_ => panic!("unknown file type"),
}
}

@ -4,7 +4,9 @@ use alloc::str;
use core::fmt::{Debug, Error, Formatter};
use core::mem::{size_of, size_of_val};
use core::slice;
use core::any::Any;
use static_assertions::const_assert;
use crate::vfs;
/// On-disk superblock
#[repr(C)]
@ -44,6 +46,15 @@ pub struct DiskINode {
pub db_indirect: u32,
}
/*
pub trait DeviceINode : Any + Sync + Send{
fn read_at(&self, _offset: usize, buf: &mut [u8]) -> vfs::Result<usize>;
fn write_at(&self, _offset: usize, buf: &[u8]) -> vfs::Result<usize>;
}
*/
pub type DeviceINode = vfs::INode;
#[repr(C)]
pub struct IndirectBlock {
pub entries: [u32; BLK_NENTRY],
@ -220,6 +231,8 @@ pub enum FileType {
File = 1,
Dir = 2,
SymLink = 3,
CharDevice = 4,
BlockDevice = 5,
}
const_assert!(o1; size_of::<SuperBlock>() <= BLKSIZE);

@ -210,6 +210,7 @@ pub enum FsError {
DirNotEmpty, //E_NOTEMPTY
WrongFs, //E_INVAL, when we find the content on disk is wrong when opening the device
DeviceError,
NoDevice
}
impl fmt::Display for FsError {

Loading…
Cancel
Save