Add comments about char/block device

master
Jiajie Chen 6 years ago
parent b421e751bb
commit e6c4590a93

@ -7,12 +7,12 @@ extern crate alloc;
extern crate log; extern crate log;
use alloc::{ use alloc::{
boxed::Box,
collections::BTreeMap, collections::BTreeMap,
string::String, string::String,
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};
@ -65,7 +65,9 @@ pub struct INodeImpl {
disk_inode: RwLock<Dirty<DiskINode>>, disk_inode: RwLock<Dirty<DiskINode>>,
/// Reference to SFS, used by almost all operations /// Reference to SFS, used by almost all operations
fs: Arc<SimpleFileSystem>, fs: Arc<SimpleFileSystem>,
device_inode_id: usize /// Char/block device id (major, minor)
/// e.g. crw-rw-rw- 1 root wheel 3, 2 May 13 16:40 /dev/null
device_inode_id: usize,
} }
impl Debug for INodeImpl { impl Debug for INodeImpl {
@ -411,10 +413,10 @@ impl vfs::INode for INodeImpl {
let device_inode = device_inodes.get(&self.device_inode_id); let device_inode = device_inodes.get(&self.device_inode_id);
match device_inode { match device_inode {
Some(device) => device.read_at(offset, buf), Some(device) => device.read_at(offset, buf),
None => Err(FsError::DeviceError) None => Err(FsError::DeviceError),
} }
}, }
_ => Err(FsError::NotFile) _ => Err(FsError::NotFile),
} }
} }
fn write_at(&self, offset: usize, buf: &[u8]) -> vfs::Result<usize> { fn write_at(&self, offset: usize, buf: &[u8]) -> vfs::Result<usize> {
@ -426,16 +428,16 @@ impl vfs::INode for INodeImpl {
self._resize(end_offset)?; self._resize(end_offset)?;
} }
self._write_at(offset, buf) self._write_at(offset, buf)
}, }
FileType::CharDevice => { FileType::CharDevice => {
let device_inodes = self.fs.device_inodes.write(); let device_inodes = self.fs.device_inodes.write();
let device_inode = device_inodes.get(&self.device_inode_id); let device_inode = device_inodes.get(&self.device_inode_id);
match device_inode { match device_inode {
Some(device) => device.write_at(offset, buf), Some(device) => device.write_at(offset, buf),
None => Err(FsError::DeviceError) None => Err(FsError::DeviceError),
} }
}, }
_ => Err(FsError::NotFile) _ => Err(FsError::NotFile),
} }
} }
fn poll(&self) -> vfs::Result<vfs::PollStatus> { fn poll(&self) -> vfs::Result<vfs::PollStatus> {
@ -678,13 +680,12 @@ impl vfs::INode for INodeImpl {
let device_inodes = self.fs.device_inodes.read(); let device_inodes = self.fs.device_inodes.read();
let device_inode = device_inodes.get(&self.device_inode_id); let device_inode = device_inodes.get(&self.device_inode_id);
match device_inode { match device_inode {
Some(x) => { x.io_control(_cmd, _data) } Some(x) => x.io_control(_cmd, _data),
None => { None => {
warn!("cannot find corresponding device inode in call_inoctl"); warn!("cannot find corresponding device inode in call_inoctl");
Err(FsError::IOCTLError) Err(FsError::IOCTLError)
} }
} }
} }
fn fs(&self) -> Arc<vfs::FileSystem> { fn fs(&self) -> Arc<vfs::FileSystem> {
self.fs.clone() self.fs.clone()
@ -692,7 +693,6 @@ impl vfs::INode for INodeImpl {
fn as_any_ref(&self) -> &Any { fn as_any_ref(&self) -> &Any {
self self
} }
} }
impl Drop for INodeImpl { impl Drop for INodeImpl {
@ -726,7 +726,7 @@ pub struct SimpleFileSystem {
/// Pointer to self, used by INodes /// Pointer to self, used by INodes
self_ptr: Weak<SimpleFileSystem>, self_ptr: Weak<SimpleFileSystem>,
/// device inode /// device inode
device_inodes: RwLock<BTreeMap<usize, Arc<DeviceINode>>> device_inodes: RwLock<BTreeMap<usize, Arc<DeviceINode>>>,
} }
impl SimpleFileSystem { impl SimpleFileSystem {
@ -751,7 +751,7 @@ impl SimpleFileSystem {
inodes: RwLock::new(BTreeMap::new()), inodes: RwLock::new(BTreeMap::new()),
device, device,
self_ptr: Weak::default(), self_ptr: Weak::default(),
device_inodes: RwLock::new(BTreeMap::new()) device_inodes: RwLock::new(BTreeMap::new()),
} }
.wrap()) .wrap())
} }
@ -783,7 +783,7 @@ impl SimpleFileSystem {
inodes: RwLock::new(BTreeMap::new()), inodes: RwLock::new(BTreeMap::new()),
device, device,
self_ptr: Weak::default(), self_ptr: Weak::default(),
device_inodes: RwLock::new(BTreeMap::new()) device_inodes: RwLock::new(BTreeMap::new()),
} }
.wrap(); .wrap();
@ -838,7 +838,9 @@ impl SimpleFileSystem {
} }
pub fn new_device_inode(&self, device_inode_id: usize, device_inode: Arc<DeviceINode>) { pub fn new_device_inode(&self, device_inode_id: usize, device_inode: Arc<DeviceINode>) {
self.device_inodes.write().insert(device_inode_id, device_inode); self.device_inodes
.write()
.insert(device_inode_id, device_inode);
} }
/// Create a new INode struct, then insert it to self.inodes /// Create a new INode struct, then insert it to self.inodes
@ -849,7 +851,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_id: device_inode_id device_inode_id: device_inode_id,
}); });
self.inodes.write().insert(id, Arc::downgrade(&inode)); self.inodes.write().insert(id, Arc::downgrade(&inode));
inode inode

@ -1,12 +1,12 @@
//! On-disk structures in SFS //! On-disk structures in SFS
use crate::vfs;
use alloc::str; use alloc::str;
use core::any::Any;
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,8 +44,8 @@ pub struct DiskINode {
pub indirect: u32, pub indirect: u32,
/// double indirect blocks /// double indirect blocks
pub db_indirect: u32, pub db_indirect: u32,
/// device inode id /// device inode id for char/block device (major, minor)
pub device_inode_id: usize pub device_inode_id: usize,
} }
/* /*
@ -136,7 +136,7 @@ impl DiskINode {
direct: [0; NDIRECT], direct: [0; NDIRECT],
indirect: 0, indirect: 0,
db_indirect: 0, db_indirect: 0,
device_inode_id: NODEVICE device_inode_id: NODEVICE,
} }
} }
pub const fn new_symlink() -> Self { pub const fn new_symlink() -> Self {
@ -148,7 +148,7 @@ impl DiskINode {
direct: [0; NDIRECT], direct: [0; NDIRECT],
indirect: 0, indirect: 0,
db_indirect: 0, db_indirect: 0,
device_inode_id: NODEVICE device_inode_id: NODEVICE,
} }
} }
pub const fn new_dir() -> Self { pub const fn new_dir() -> Self {
@ -160,7 +160,7 @@ impl DiskINode {
direct: [0; NDIRECT], direct: [0; NDIRECT],
indirect: 0, indirect: 0,
db_indirect: 0, db_indirect: 0,
device_inode_id: NODEVICE device_inode_id: NODEVICE,
} }
} }
pub const fn new_chardevice(device_inode_id: usize) -> Self { pub const fn new_chardevice(device_inode_id: usize) -> Self {
@ -172,7 +172,7 @@ impl DiskINode {
direct: [0; NDIRECT], direct: [0; NDIRECT],
indirect: 0, indirect: 0,
db_indirect: 0, db_indirect: 0,
device_inode_id: device_inode_id device_inode_id: device_inode_id,
} }
} }
} }

@ -138,14 +138,13 @@ impl INode {
} }
Ok(result) Ok(result)
} }
} }
pub enum IOCTLError { pub enum IOCTLError {
NotValidFD = 9, // EBADF NotValidFD = 9, // EBADF
NotValidMemory = 14, // EFAULT NotValidMemory = 14, // EFAULT
NotValidParam = 22, // EINVAL NotValidParam = 22, // EINVAL
NotCharDevice = 25, // ENOTTY NotCharDevice = 25, // ENOTTY
} }
#[derive(Debug, Default)] #[derive(Debug, Default)]
@ -253,7 +252,7 @@ pub enum FsError {
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,
IOCTLError, IOCTLError,
NoDevice NoDevice,
} }
impl fmt::Display for FsError { impl fmt::Display for FsError {

Loading…
Cancel
Save