Make SFS interior mutable

master
WangRunji 7 years ago
parent a87851d2f6
commit c04bed4867

@ -53,7 +53,7 @@ pub struct INode {
/// inode number /// inode number
id: INodeId, id: INodeId,
/// Weak reference to SFS, used by almost all operations /// Weak reference to SFS, used by almost all operations
fs: WeakPtr<SimpleFileSystem>, fs: Weak<SimpleFileSystem>,
} }
impl Debug for INode { impl Debug for INode {
@ -73,7 +73,7 @@ impl INode {
id if id < NDIRECT + BLK_NENTRY => { id if id < NDIRECT + BLK_NENTRY => {
let mut disk_block_id: u32 = 0; let mut disk_block_id: u32 = 0;
let fs = self.fs.upgrade().unwrap(); let fs = self.fs.upgrade().unwrap();
fs.borrow_mut().device.read_block( fs.device.borrow_mut().read_block(
self.disk_inode.indirect as usize, self.disk_inode.indirect as usize,
ENTRY_SIZE * (id - NDIRECT), ENTRY_SIZE * (id - NDIRECT),
disk_block_id.as_buf_mut(), disk_block_id.as_buf_mut(),
@ -94,7 +94,7 @@ impl INode {
id if id < NDIRECT + BLK_NENTRY => { id if id < NDIRECT + BLK_NENTRY => {
let disk_block_id = disk_block_id as u32; let disk_block_id = disk_block_id as u32;
let fs = self.fs.upgrade().unwrap(); let fs = self.fs.upgrade().unwrap();
fs.borrow_mut().device.write_block( fs.device.borrow_mut().write_block(
self.disk_inode.indirect as usize, self.disk_inode.indirect as usize,
ENTRY_SIZE * (id - NDIRECT), ENTRY_SIZE * (id - NDIRECT),
disk_block_id.as_buf(), disk_block_id.as_buf(),
@ -139,7 +139,7 @@ impl INode {
for BlockRange { block, begin, end } in iter { for BlockRange { block, begin, end } in iter {
static ZEROS: [u8; BLKSIZE] = [0; BLKSIZE]; static ZEROS: [u8; BLKSIZE] = [0; BLKSIZE];
let disk_block_id = self.get_disk_block_id(block).unwrap(); let disk_block_id = self.get_disk_block_id(block).unwrap();
fs.borrow_mut().device.write_block(disk_block_id, begin, &ZEROS[begin..end]).unwrap(); fs.device.borrow_mut().write_block(disk_block_id, begin, &ZEROS[begin..end]).unwrap();
} }
Ok(()) Ok(())
} }
@ -166,7 +166,7 @@ impl vfs::INode for INode {
for BlockRange { block, begin, end } in iter { for BlockRange { block, begin, end } in iter {
let disk_block_id = self.get_disk_block_id(block).unwrap(); let disk_block_id = self.get_disk_block_id(block).unwrap();
let len = end - begin; let len = end - begin;
fs.borrow_mut().device.read_block(disk_block_id, begin, &mut buf[buf_offset..buf_offset + len]).unwrap(); fs.device.borrow_mut().read_block(disk_block_id, begin, &mut buf[buf_offset..buf_offset + len]).unwrap();
buf_offset += len; buf_offset += len;
} }
Ok(buf_offset) Ok(buf_offset)
@ -184,7 +184,7 @@ impl vfs::INode for INode {
for BlockRange { block, begin, end } in iter { for BlockRange { block, begin, end } in iter {
let disk_block_id = self.get_disk_block_id(block).unwrap(); let disk_block_id = self.get_disk_block_id(block).unwrap();
let len = end - begin; let len = end - begin;
fs.borrow_mut().device.write_block(disk_block_id, begin, &buf[buf_offset..buf_offset + len]).unwrap(); fs.device.borrow_mut().write_block(disk_block_id, begin, &buf[buf_offset..buf_offset + len]).unwrap();
buf_offset += len; buf_offset += len;
} }
Ok(buf_offset) Ok(buf_offset)
@ -199,7 +199,7 @@ impl vfs::INode for INode {
fn sync(&mut self) -> vfs::Result<()> { fn sync(&mut self) -> vfs::Result<()> {
if self.disk_inode.dirty() { if self.disk_inode.dirty() {
let fs = self.fs.upgrade().unwrap(); let fs = self.fs.upgrade().unwrap();
fs.borrow_mut().device.write_block(self.id, 0, self.disk_inode.as_buf()).unwrap(); fs.device.borrow_mut().write_block(self.id, 0, self.disk_inode.as_buf()).unwrap();
self.disk_inode.sync(); self.disk_inode.sync();
} }
Ok(()) Ok(())
@ -218,11 +218,11 @@ impl vfs::INode for INode {
self.disk_inode.blocks = blocks; self.disk_inode.blocks = blocks;
// allocate indirect block if need // allocate indirect block if need
if old_blocks < NDIRECT as u32 && blocks >= NDIRECT as u32 { if old_blocks < NDIRECT as u32 && blocks >= NDIRECT as u32 {
self.disk_inode.indirect = fs.borrow_mut().alloc_block().unwrap() as u32; self.disk_inode.indirect = fs.alloc_block().unwrap() as u32;
} }
// allocate extra blocks // allocate extra blocks
for i in old_blocks .. blocks { for i in old_blocks .. blocks {
let disk_block_id = fs.borrow_mut().alloc_block().expect("no more space"); let disk_block_id = fs.alloc_block().expect("no more space");
self.set_disk_block_id(i as usize, disk_block_id).unwrap(); self.set_disk_block_id(i as usize, disk_block_id).unwrap();
} }
// clean up // clean up
@ -234,11 +234,11 @@ impl vfs::INode for INode {
// free extra blocks // free extra blocks
for i in blocks .. self.disk_inode.blocks { for i in blocks .. self.disk_inode.blocks {
let disk_block_id = self.get_disk_block_id(i as usize).unwrap(); let disk_block_id = self.get_disk_block_id(i as usize).unwrap();
fs.borrow_mut().free_block(disk_block_id); fs.free_block(disk_block_id);
} }
// free indirect block if need // free indirect block if need
if blocks < NDIRECT as u32 && self.disk_inode.blocks >= NDIRECT as u32 { if blocks < NDIRECT as u32 && self.disk_inode.blocks >= NDIRECT as u32 {
fs.borrow_mut().free_block(self.disk_inode.indirect as usize); fs.free_block(self.disk_inode.indirect as usize);
self.disk_inode.indirect = 0; self.disk_inode.indirect = 0;
} }
self.disk_inode.blocks = blocks; self.disk_inode.blocks = blocks;
@ -257,7 +257,7 @@ impl vfs::INode for INode {
assert!(self.get_file_inode_id(name).is_none(), "file name exist"); assert!(self.get_file_inode_id(name).is_none(), "file name exist");
// Create new INode // Create new INode
let inode = fs.borrow_mut().new_inode_file().unwrap(); let inode = fs.new_inode_file().unwrap();
// Write new entry // Write new entry
let entry = DiskEntry { let entry = DiskEntry {
@ -283,7 +283,7 @@ impl vfs::INode for INode {
if inode_id.is_none() { if inode_id.is_none() {
return Err(()); return Err(());
} }
let inode = fs.borrow_mut().get_inode(inode_id.unwrap()); let inode = fs.get_inode(inode_id.unwrap());
let type_ = inode.borrow().disk_inode.type_; let type_ = inode.borrow().disk_inode.type_;
match type_ { match type_ {
@ -332,22 +332,27 @@ impl Iterator for BlockIter {
/// filesystem for sfs /// filesystem for sfs
///
/// ## 内部可变性
/// 为了方便协调外部及INode对SFS的访问并为日后并行化做准备
/// 将SFS设置为内部可变即对外接口全部是&selfstruct的全部field用RefCell包起来
/// 这样其内部各field均可独立访问
pub struct SimpleFileSystem { pub struct SimpleFileSystem {
/// on-disk superblock /// on-disk superblock
super_block: Dirty<SuperBlock>, super_block: RefCell<Dirty<SuperBlock>>,
/// blocks in use are mared 0 /// blocks in use are mared 0
free_map: Dirty<BitSet>, free_map: RefCell<Dirty<BitSet>>,
/// inode list /// inode list
inodes: BTreeMap<INodeId, Ptr<INode>>, inodes: RefCell<BTreeMap<INodeId, Ptr<INode>>>,
/// device /// device
device: Box<Device>, device: RefCell<Box<Device>>,
/// Pointer to self, used by INodes /// Pointer to self, used by INodes
self_ptr: WeakPtr<SimpleFileSystem>, self_ptr: Weak<SimpleFileSystem>,
} }
impl SimpleFileSystem { impl SimpleFileSystem {
/// Load SFS from device /// Load SFS from device
pub fn open(mut device: Box<Device>) -> Option<Ptr<Self>> { pub fn open(mut device: Box<Device>) -> Option<Rc<Self>> {
let super_block = device.load_struct::<SuperBlock>(BLKN_SUPER); let super_block = device.load_struct::<SuperBlock>(BLKN_SUPER);
if super_block.check() == false { if super_block.check() == false {
return None; return None;
@ -355,15 +360,15 @@ impl SimpleFileSystem {
let free_map = device.load_struct::<[u8; BLKSIZE]>(BLKN_FREEMAP); let free_map = device.load_struct::<[u8; BLKSIZE]>(BLKN_FREEMAP);
Some(SimpleFileSystem { Some(SimpleFileSystem {
super_block: Dirty::new(super_block), super_block: RefCell::new(Dirty::new(super_block)),
free_map: Dirty::new(BitSet::from_bytes(&free_map)), free_map: RefCell::new(Dirty::new(BitSet::from_bytes(&free_map))),
inodes: BTreeMap::<INodeId, Ptr<INode>>::new(), inodes: RefCell::new(BTreeMap::<INodeId, Ptr<INode>>::new()),
device, device: RefCell::new(device),
self_ptr: Weak::default(), self_ptr: Weak::default(),
}.wrap()) }.wrap())
} }
/// Create a new SFS on blank disk /// Create a new SFS on blank disk
pub fn create(mut device: Box<Device>, space: usize) -> Ptr<Self> { pub fn create(mut device: Box<Device>, space: usize) -> Rc<Self> {
let blocks = (space / BLKSIZE).min(BLKBITS); let blocks = (space / BLKSIZE).min(BLKBITS);
assert!(blocks >= 16, "space too small"); assert!(blocks >= 16, "space too small");
@ -382,10 +387,10 @@ impl SimpleFileSystem {
}; };
let sfs = SimpleFileSystem { let sfs = SimpleFileSystem {
super_block: Dirty::new_dirty(super_block), super_block: RefCell::new(Dirty::new_dirty(super_block)),
free_map: Dirty::new_dirty(free_map), free_map: RefCell::new(Dirty::new_dirty(free_map)),
inodes: BTreeMap::<INodeId, Ptr<INode>>::new(), inodes: RefCell::new(BTreeMap::<INodeId, Ptr<INode>>::new()),
device, device: RefCell::new(device),
self_ptr: Weak::default(), self_ptr: Weak::default(),
}.wrap(); }.wrap();
@ -400,53 +405,64 @@ impl SimpleFileSystem {
use vfs::INode; use vfs::INode;
inode.borrow_mut().sync().unwrap(); inode.borrow_mut().sync().unwrap();
} }
sfs.borrow_mut().inodes.insert(BLKN_ROOT, inode); sfs.inodes.borrow_mut().insert(BLKN_ROOT, inode);
sfs sfs
} }
/// Wrap pure SimpleFileSystem with Rc<RefCell<...>> /// Wrap pure SimpleFileSystem with Rc
/// Used in constructors /// Used in constructors
fn wrap(self) -> Ptr<Self> { fn wrap(self) -> Rc<Self> {
let mut fs = Rc::new(RefCell::new(self)); // Create a Rc, make a Weak from it, then put it into the struct.
fs.borrow_mut().self_ptr = Rc::downgrade(&fs); // It's a little tricky.
let mut fs = Rc::new(self);
// Force create a reference to make Weak
let fs1 = unsafe{ &*(&fs as *const Rc<SimpleFileSystem>) };
{
// `Rc::get_mut` is allowed when there is only one strong reference
// So the following 2 lines can not be joint.
let fs0 = Rc::get_mut(&mut fs).unwrap();
fs0.self_ptr = Rc::downgrade(&fs1);
}
fs fs
} }
/// Allocate a block, return block id /// Allocate a block, return block id
fn alloc_block(&mut self) -> Option<usize> { fn alloc_block(&self) -> Option<usize> {
let id = self.free_map.alloc(); let id = self.free_map.borrow_mut().alloc();
if id.is_some() { if id.is_some() {
self.super_block.unused_blocks -= 1; // will panic if underflow self.super_block.borrow_mut().unused_blocks -= 1; // will panic if underflow
} }
id id
} }
/// Free a block /// Free a block
fn free_block(&mut self, block_id: usize) { fn free_block(&self, block_id: usize) {
assert!(!self.free_map.contains(block_id)); let mut free_map = self.free_map.borrow_mut();
self.free_map.insert(block_id); assert!(!free_map.contains(block_id));
self.super_block.unused_blocks += 1; free_map.insert(block_id);
self.super_block.borrow_mut().unused_blocks += 1;
} }
/// Get inode by id. Load if not in memory. /// Get inode by id. Load if not in memory.
/// ** Must ensure it's a valid INode ** /// ** Must ensure it's a valid INode **
fn get_inode(&mut self, id: INodeId) -> Ptr<INode> { fn get_inode(&self, id: INodeId) -> Ptr<INode> {
assert!(!self.free_map.contains(id)); assert!(!self.free_map.borrow().contains(id));
let mut inodes = self.inodes.borrow_mut();
// Load if not in memory. // Load if not in memory.
if !self.inodes.contains_key(&id) { if !inodes.contains_key(&id) {
let disk_inode = self.device.load_struct::<DiskINode>(id); let disk_inode = self.device.borrow_mut().load_struct::<DiskINode>(id);
let inode = Rc::new(RefCell::new(INode { let inode = Rc::new(RefCell::new(INode {
disk_inode: Dirty::new(disk_inode), disk_inode: Dirty::new(disk_inode),
id, id,
fs: self.self_ptr.clone(), fs: self.self_ptr.clone(),
})); }));
self.inodes.insert(id, inode.clone()); inodes.insert(id, inode.clone());
inode inode
} else { } else {
self.inodes.get(&id).unwrap().clone() inodes.get(&id).unwrap().clone()
} }
} }
/// Create a new INode file /// Create a new INode file
fn new_inode_file(&mut self) -> vfs::Result<Ptr<INode>> { fn new_inode_file(&self) -> vfs::Result<Ptr<INode>> {
let id = self.alloc_block().unwrap(); let id = self.alloc_block().unwrap();
Ok(Rc::new(RefCell::new(INode { Ok(Rc::new(RefCell::new(INode {
disk_inode: Dirty::new_dirty(DiskINode::new_file()), disk_inode: Dirty::new_dirty(DiskINode::new_file()),
@ -455,7 +471,7 @@ impl SimpleFileSystem {
}))) })))
} }
/// Create a new INode dir /// Create a new INode dir
fn new_inode_dir(&mut self, parent: INodeId) -> vfs::Result<Ptr<INode>> { fn new_inode_dir(&self, parent: INodeId) -> vfs::Result<Ptr<INode>> {
let id = self.alloc_block().unwrap(); let id = self.alloc_block().unwrap();
let mut inode = INode { let mut inode = INode {
disk_inode: Dirty::new_dirty(DiskINode::new_dir()), disk_inode: Dirty::new_dirty(DiskINode::new_dir()),
@ -472,41 +488,31 @@ impl vfs::FileSystem for SimpleFileSystem {
type INode = INode; type INode = INode;
/// Write back super block if dirty /// Write back super block if dirty
fn sync(&mut self) -> vfs::Result<()> { fn sync(&self) -> vfs::Result<()> {
let SimpleFileSystem { {
ref mut super_block, let mut super_block = self.super_block.borrow_mut();
ref mut device, if super_block.dirty() {
ref mut free_map, self.device.borrow_mut().write_at(BLKSIZE * BLKN_SUPER, super_block.as_buf()).unwrap();
ref mut inodes, super_block.sync();
.. }
} = self;
if super_block.dirty() {
device.write_at(BLKSIZE * BLKN_SUPER, super_block.as_buf()).unwrap();
super_block.sync();
} }
if free_map.dirty() { {
device.write_at(BLKSIZE * BLKN_FREEMAP, free_map.as_buf()).unwrap(); let mut free_map = self.free_map.borrow_mut();
free_map.sync(); if free_map.dirty() {
self.device.borrow_mut().write_at(BLKSIZE * BLKN_FREEMAP, free_map.as_buf()).unwrap();
free_map.sync();
}
} }
for inode in inodes.values() { for inode in self.inodes.borrow().values() {
use vfs::INode; use vfs::INode;
inode.borrow_mut().sync().unwrap(); inode.borrow_mut().sync().unwrap();
} }
Ok(()) Ok(())
} }
fn root_inode(&mut self) -> Ptr<INode> { fn root_inode(&self) -> Ptr<INode> {
self.get_inode(BLKN_ROOT) self.get_inode(BLKN_ROOT)
} }
fn unmount(&mut self) -> vfs::Result<()> {
unimplemented!()
}
fn cleanup(&mut self) {
unimplemented!()
}
} }
impl Drop for SimpleFileSystem { impl Drop for SimpleFileSystem {

@ -29,7 +29,7 @@ fn test() {
.expect("failed to open sfs.img"); .expect("failed to open sfs.img");
let sfs = SimpleFileSystem::open(Box::new(file)) let sfs = SimpleFileSystem::open(Box::new(file))
.expect("failed to create SFS"); .expect("failed to create SFS");
let root = sfs.borrow_mut().root_inode(); let root = sfs.root_inode();
println!("{:?}", root); println!("{:?}", root);
use super::structs::{DiskEntry, AsBuf}; use super::structs::{DiskEntry, AsBuf};
@ -47,7 +47,7 @@ fn create() {
.read(true).write(true).create(true).open("test.img") .read(true).write(true).create(true).open("test.img")
.expect("failed to create file"); .expect("failed to create file");
let sfs = SimpleFileSystem::create(Box::new(file), 16 * 4096); let sfs = SimpleFileSystem::create(Box::new(file), 16 * 4096);
let root = sfs.borrow_mut().root_inode(); let root = sfs.root_inode();
let file1 = root.borrow_mut().create("file1").unwrap(); let file1 = root.borrow_mut().create("file1").unwrap();
sfs.borrow_mut().sync().unwrap(); sfs.sync().unwrap();
} }

@ -37,8 +37,8 @@ pub type Result<T> = core::result::Result<T, ()>;
/// Abstract filesystem /// Abstract filesystem
pub trait FileSystem { pub trait FileSystem {
type INode: INode; type INode: INode;
fn sync(&mut self) -> Result<()>; fn sync(&self) -> Result<()>;
fn root_inode(&mut self) -> Rc<RefCell<Self::INode>>; fn root_inode(&self) -> Rc<RefCell<Self::INode>>;
fn unmount(&mut self) -> Result<()>; // fn unmount(&self) -> Result<()>;
fn cleanup(&mut self); // fn cleanup(&self);
} }
Loading…
Cancel
Save