|
|
|
@ -41,6 +41,9 @@ fn load_struct<T: AsBuf>(device: &mut Device, id: BlockId) -> T {
|
|
|
|
|
s
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type Ptr<T> = Rc<RefCell<T>>;
|
|
|
|
|
type WeakPtr<T> = Weak<RefCell<T>>;
|
|
|
|
|
|
|
|
|
|
/// inode for sfs
|
|
|
|
|
pub struct INode {
|
|
|
|
|
/// on-disk inode
|
|
|
|
@ -48,7 +51,7 @@ pub struct INode {
|
|
|
|
|
/// inode number
|
|
|
|
|
id: INodeId,
|
|
|
|
|
/// Weak reference to SFS, used by almost all operations
|
|
|
|
|
fs: Weak<RefCell<SimpleFileSystem>>,
|
|
|
|
|
fs: WeakPtr<SimpleFileSystem>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Debug for INode {
|
|
|
|
@ -71,10 +74,10 @@ impl INode {
|
|
|
|
|
fs.borrow_mut().device.read_block(
|
|
|
|
|
self.disk_inode.indirect as usize,
|
|
|
|
|
ENTRY_SIZE * (id - NDIRECT),
|
|
|
|
|
disk_block_id.as_buf_mut()
|
|
|
|
|
disk_block_id.as_buf_mut(),
|
|
|
|
|
).unwrap();
|
|
|
|
|
Some(disk_block_id as BlockId)
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
id => unimplemented!("double indirect blocks is not supported"),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -134,6 +137,15 @@ impl vfs::INode for INode {
|
|
|
|
|
}
|
|
|
|
|
Some(buf_offset)
|
|
|
|
|
}
|
|
|
|
|
fn info(&mut self) -> Result<vfs::FileInfo, ()> {
|
|
|
|
|
if self.disk_inode.type_ != FileType::File {
|
|
|
|
|
return Err(());
|
|
|
|
|
}
|
|
|
|
|
Ok(vfs::FileInfo {
|
|
|
|
|
size: self.disk_inode.size as usize,
|
|
|
|
|
mode: 0,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
fn sync(&mut self) -> Result<(), ()> {
|
|
|
|
|
if self.disk_inode.dirty() {
|
|
|
|
|
let fs0 = self.fs.upgrade().unwrap();
|
|
|
|
@ -143,6 +155,9 @@ impl vfs::INode for INode {
|
|
|
|
|
}
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
fn type_(&self) -> Result<u32, ()> {
|
|
|
|
|
Ok(self.disk_inode.type_.clone() as u32)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Given a range and iterate sub-range for each block
|
|
|
|
@ -180,16 +195,16 @@ pub struct SimpleFileSystem {
|
|
|
|
|
/// blocks in use are mared 0
|
|
|
|
|
free_map: BitSet,
|
|
|
|
|
/// inode list
|
|
|
|
|
inodes: BTreeMap<INodeId, Rc<INode>>,
|
|
|
|
|
inodes: BTreeMap<INodeId, Ptr<INode>>,
|
|
|
|
|
/// device
|
|
|
|
|
device: Box<Device>,
|
|
|
|
|
/// Pointer to self, used by INodes
|
|
|
|
|
self_ptr: Weak<RefCell<SimpleFileSystem>>,
|
|
|
|
|
self_ptr: WeakPtr<SimpleFileSystem>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl SimpleFileSystem {
|
|
|
|
|
/// Create a new SFS with device
|
|
|
|
|
pub fn new(mut device: Box<Device>) -> Option<Rc<RefCell<Self>>> {
|
|
|
|
|
pub fn new(mut device: Box<Device>) -> Option<Ptr<Self>> {
|
|
|
|
|
let super_block = load_struct::<SuperBlock>(device.as_mut(), BLKN_SUPER);
|
|
|
|
|
if super_block.check() == false {
|
|
|
|
|
return None;
|
|
|
|
@ -198,7 +213,7 @@ impl SimpleFileSystem {
|
|
|
|
|
let mut fs = Rc::new(RefCell::new(SimpleFileSystem {
|
|
|
|
|
super_block: Dirty::new(super_block),
|
|
|
|
|
free_map: BitSet::new(),
|
|
|
|
|
inodes: BTreeMap::<INodeId, Rc<INode>>::new(),
|
|
|
|
|
inodes: BTreeMap::<INodeId, Ptr<INode>>::new(),
|
|
|
|
|
device,
|
|
|
|
|
self_ptr: Weak::default(),
|
|
|
|
|
}));
|
|
|
|
@ -223,17 +238,17 @@ impl SimpleFileSystem {
|
|
|
|
|
|
|
|
|
|
/// Get inode by id. Load if not in memory.
|
|
|
|
|
/// ** Must ensure it's a valid INode **
|
|
|
|
|
fn get_inode(&mut self, id: INodeId) -> Rc<INode> {
|
|
|
|
|
fn get_inode(&mut self, id: INodeId) -> Ptr<INode> {
|
|
|
|
|
assert!(!self.free_map.contains(id));
|
|
|
|
|
|
|
|
|
|
// Load if not in memory.
|
|
|
|
|
if !self.inodes.contains_key(&id) {
|
|
|
|
|
let disk_inode = load_struct::<DiskINode>(self.device.as_mut(), id);
|
|
|
|
|
let inode = Rc::new(INode {
|
|
|
|
|
let inode = Rc::new(RefCell::new(INode {
|
|
|
|
|
disk_inode: Dirty::new(disk_inode),
|
|
|
|
|
id,
|
|
|
|
|
fs: self.self_ptr.clone(),
|
|
|
|
|
});
|
|
|
|
|
}));
|
|
|
|
|
self.inodes.insert(id, inode.clone());
|
|
|
|
|
inode
|
|
|
|
|
} else {
|
|
|
|
@ -260,7 +275,7 @@ impl vfs::FileSystem for SimpleFileSystem {
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn root_inode(&mut self) -> Rc<INode> {
|
|
|
|
|
fn root_inode(&mut self) -> Ptr<INode> {
|
|
|
|
|
self.get_inode(BLKN_ROOT)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|