trick to add device inode

master
gaotianyu1350 6 years ago
parent 1cc5fd90bb
commit de6b914cd1

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

@ -4,7 +4,9 @@ use alloc::str;
use core::fmt::{Debug, Error, Formatter}; use core::fmt::{Debug, Error, Formatter};
use core::mem::{size_of, size_of_val}; use core::mem::{size_of, size_of_val};
use core::slice; use core::slice;
use core::any::Any;
use static_assertions::const_assert; use static_assertions::const_assert;
use crate::vfs;
/// On-disk superblock /// On-disk superblock
#[repr(C)] #[repr(C)]
@ -44,6 +46,15 @@ pub struct DiskINode {
pub db_indirect: u32, 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)] #[repr(C)]
pub struct IndirectBlock { pub struct IndirectBlock {
pub entries: [u32; BLK_NENTRY], pub entries: [u32; BLK_NENTRY],
@ -220,6 +231,8 @@ pub enum FileType {
File = 1, File = 1,
Dir = 2, Dir = 2,
SymLink = 3, SymLink = 3,
CharDevice = 4,
BlockDevice = 5,
} }
const_assert!(o1; size_of::<SuperBlock>() <= BLKSIZE); const_assert!(o1; size_of::<SuperBlock>() <= BLKSIZE);

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

Loading…
Cancel
Save