|
|
@ -17,14 +17,14 @@ pub trait Device {
|
|
|
|
|
|
|
|
|
|
|
|
// Helper functions
|
|
|
|
// Helper functions
|
|
|
|
|
|
|
|
|
|
|
|
fn read_block(&mut self, id: BlockId, offset: usize, buf: &mut [u8]) -> Result<(), ()> {
|
|
|
|
fn read_block(&mut self, id: BlockId, offset: usize, buf: &mut [u8]) -> vfs::Result<()> {
|
|
|
|
debug_assert!(offset + buf.len() <= BLKSIZE);
|
|
|
|
debug_assert!(offset + buf.len() <= BLKSIZE);
|
|
|
|
match self.read_at(id * BLKSIZE + offset, buf) {
|
|
|
|
match self.read_at(id * BLKSIZE + offset, buf) {
|
|
|
|
Some(len) if len == buf.len() => Ok(()),
|
|
|
|
Some(len) if len == buf.len() => Ok(()),
|
|
|
|
_ => Err(()),
|
|
|
|
_ => Err(()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fn write_block(&mut self, id: BlockId, offset: usize, buf: &[u8]) -> Result<(), ()> {
|
|
|
|
fn write_block(&mut self, id: BlockId, offset: usize, buf: &[u8]) -> vfs::Result<()> {
|
|
|
|
debug_assert!(offset + buf.len() <= BLKSIZE);
|
|
|
|
debug_assert!(offset + buf.len() <= BLKSIZE);
|
|
|
|
match self.write_at(id * BLKSIZE + offset, buf) {
|
|
|
|
match self.write_at(id * BLKSIZE + offset, buf) {
|
|
|
|
Some(len) if len == buf.len() => Ok(()),
|
|
|
|
Some(len) if len == buf.len() => Ok(()),
|
|
|
@ -33,13 +33,15 @@ pub trait Device {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Load struct `T` from given block in device
|
|
|
|
trait DeviceExt: Device {
|
|
|
|
/// Workaround: It should be inside the trait `Device`. But that would cause compile error.
|
|
|
|
/// Load struct `T` from given block in device
|
|
|
|
fn load_struct<T: AsBuf>(device: &mut Device, id: BlockId) -> T {
|
|
|
|
fn load_struct<T: AsBuf>(&mut self, id: BlockId) -> T {
|
|
|
|
let mut s: T = unsafe { uninitialized() };
|
|
|
|
let mut s: T = unsafe { uninitialized() };
|
|
|
|
device.read_block(id, 0, s.as_buf_mut()).unwrap();
|
|
|
|
self.read_block(id, 0, s.as_buf_mut()).unwrap();
|
|
|
|
s
|
|
|
|
s
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DeviceExt for Device {}
|
|
|
|
|
|
|
|
|
|
|
|
type Ptr<T> = Rc<RefCell<T>>;
|
|
|
|
type Ptr<T> = Rc<RefCell<T>>;
|
|
|
|
type WeakPtr<T> = Weak<RefCell<T>>;
|
|
|
|
type WeakPtr<T> = Weak<RefCell<T>>;
|
|
|
@ -81,7 +83,7 @@ impl INode {
|
|
|
|
id => unimplemented!("double indirect blocks is not supported"),
|
|
|
|
id => unimplemented!("double indirect blocks is not supported"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fn set_disk_block_id(&mut self, file_block_id: BlockId, disk_block_id: BlockId) -> Result<(),()> {
|
|
|
|
fn set_disk_block_id(&mut self, file_block_id: BlockId, disk_block_id: BlockId) -> vfs::Result<()> {
|
|
|
|
match file_block_id {
|
|
|
|
match file_block_id {
|
|
|
|
id if id >= self.disk_inode.blocks as BlockId =>
|
|
|
|
id if id >= self.disk_inode.blocks as BlockId =>
|
|
|
|
Err(()),
|
|
|
|
Err(()),
|
|
|
@ -102,17 +104,56 @@ impl INode {
|
|
|
|
id => unimplemented!("double indirect blocks is not supported"),
|
|
|
|
id => unimplemented!("double indirect blocks is not supported"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Only for Dir
|
|
|
|
|
|
|
|
fn get_file_inode_id(&mut self, name: &'static str) -> Option<INodeId> {
|
|
|
|
|
|
|
|
(0 .. self.disk_inode.blocks)
|
|
|
|
|
|
|
|
.map(|i| {
|
|
|
|
|
|
|
|
use vfs::INode;
|
|
|
|
|
|
|
|
let mut entry: DiskEntry = unsafe {uninitialized()};
|
|
|
|
|
|
|
|
self.read_at(i as usize * BLKSIZE, entry.as_buf_mut()).unwrap();
|
|
|
|
|
|
|
|
entry
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
.find(|entry| entry.name.as_ref() == name)
|
|
|
|
|
|
|
|
.map(|entry| entry.id as INodeId)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Init dir content. Insert 2 init entries.
|
|
|
|
|
|
|
|
fn init_dir(&mut self, parent: INodeId) -> vfs::Result<()> {
|
|
|
|
|
|
|
|
use vfs::INode;
|
|
|
|
|
|
|
|
// Insert entries: '.' '..'
|
|
|
|
|
|
|
|
self.resize(BLKSIZE * 2).unwrap();
|
|
|
|
|
|
|
|
self.write_at(0, DiskEntry {
|
|
|
|
|
|
|
|
id: parent as u32,
|
|
|
|
|
|
|
|
name: Str256::from(".."),
|
|
|
|
|
|
|
|
}.as_buf()).unwrap();
|
|
|
|
|
|
|
|
let id = self.id as u32;
|
|
|
|
|
|
|
|
self.write_at(0, DiskEntry {
|
|
|
|
|
|
|
|
id,
|
|
|
|
|
|
|
|
name: Str256::from("."),
|
|
|
|
|
|
|
|
}.as_buf()).unwrap();
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
fn clean_at(&mut self, begin: usize, end: usize) -> vfs::Result<()> {
|
|
|
|
|
|
|
|
let fs = self.fs.upgrade().unwrap();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let iter = BlockIter { begin, end };
|
|
|
|
|
|
|
|
for BlockRange { block, begin, end } in iter {
|
|
|
|
|
|
|
|
static ZEROS: [u8; BLKSIZE] = [0; BLKSIZE];
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl vfs::INode for INode {
|
|
|
|
impl vfs::INode for INode {
|
|
|
|
fn open(&mut self, flags: u32) -> Result<(), ()> {
|
|
|
|
fn open(&mut self, flags: u32) -> vfs::Result<()> {
|
|
|
|
// Do nothing
|
|
|
|
// Do nothing
|
|
|
|
Ok(())
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fn close(&mut self) -> Result<(), ()> {
|
|
|
|
fn close(&mut self) -> vfs::Result<()> {
|
|
|
|
self.sync()
|
|
|
|
self.sync()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fn read_at(&mut self, offset: usize, buf: &mut [u8]) -> Option<usize> {
|
|
|
|
fn read_at(&mut self, offset: usize, buf: &mut [u8]) -> vfs::Result<usize> {
|
|
|
|
let fs = self.fs.upgrade().unwrap();
|
|
|
|
let fs = self.fs.upgrade().unwrap();
|
|
|
|
|
|
|
|
|
|
|
|
let iter = BlockIter {
|
|
|
|
let iter = BlockIter {
|
|
|
@ -123,18 +164,14 @@ impl vfs::INode for INode {
|
|
|
|
// Read for each block
|
|
|
|
// Read for each block
|
|
|
|
let mut buf_offset = 0usize;
|
|
|
|
let mut buf_offset = 0usize;
|
|
|
|
for BlockRange { block, begin, end } in iter {
|
|
|
|
for BlockRange { block, begin, end } in iter {
|
|
|
|
if let Some(disk_block_id) = self.get_disk_block_id(block) {
|
|
|
|
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.borrow_mut().device.read_block(disk_block_id, begin, &mut buf[buf_offset..buf_offset + len]).unwrap();
|
|
|
|
buf_offset += len;
|
|
|
|
buf_offset += len;
|
|
|
|
} else {
|
|
|
|
|
|
|
|
// Failed this time
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(buf_offset)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Some(buf_offset)
|
|
|
|
fn write_at(&mut self, offset: usize, buf: &[u8]) -> vfs::Result<usize> {
|
|
|
|
}
|
|
|
|
|
|
|
|
fn write_at(&mut self, offset: usize, buf: &[u8]) -> Option<usize> {
|
|
|
|
|
|
|
|
let fs = self.fs.upgrade().unwrap();
|
|
|
|
let fs = self.fs.upgrade().unwrap();
|
|
|
|
|
|
|
|
|
|
|
|
let iter = BlockIter {
|
|
|
|
let iter = BlockIter {
|
|
|
@ -142,43 +179,33 @@ impl vfs::INode for INode {
|
|
|
|
end: offset + buf.len(),
|
|
|
|
end: offset + buf.len(),
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// Read for each block
|
|
|
|
// Write for each block
|
|
|
|
let mut buf_offset = 0usize;
|
|
|
|
let mut buf_offset = 0usize;
|
|
|
|
for BlockRange { block, begin, end } in iter {
|
|
|
|
for BlockRange { block, begin, end } in iter {
|
|
|
|
if let Some(disk_block_id) = self.get_disk_block_id(block) {
|
|
|
|
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.borrow_mut().device.write_block(disk_block_id, begin, &buf[buf_offset..buf_offset + len]).unwrap();
|
|
|
|
buf_offset += len;
|
|
|
|
buf_offset += len;
|
|
|
|
} else {
|
|
|
|
|
|
|
|
// Failed this time
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Some(buf_offset)
|
|
|
|
Ok(buf_offset)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn info(&mut self) -> Result<vfs::FileInfo, ()> {
|
|
|
|
|
|
|
|
if self.disk_inode.type_ != FileType::File {
|
|
|
|
|
|
|
|
return Err(());
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn info(&mut self) -> vfs::Result<vfs::FileInfo> {
|
|
|
|
Ok(vfs::FileInfo {
|
|
|
|
Ok(vfs::FileInfo {
|
|
|
|
size: self.disk_inode.size as usize,
|
|
|
|
size: self.disk_inode.size as usize,
|
|
|
|
mode: 0,
|
|
|
|
mode: 0,
|
|
|
|
|
|
|
|
type_: vfs::FileType::from(self.disk_inode.type_.clone()),
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fn sync(&mut self) -> Result<(), ()> {
|
|
|
|
fn sync(&mut self) -> vfs::Result<()> {
|
|
|
|
if self.disk_inode.dirty() {
|
|
|
|
if self.disk_inode.dirty() {
|
|
|
|
let fs0 = self.fs.upgrade().unwrap();
|
|
|
|
let fs = self.fs.upgrade().unwrap();
|
|
|
|
let mut fs = fs0.borrow_mut();
|
|
|
|
fs.borrow_mut().device.write_block(self.id, 0, self.disk_inode.as_buf()).unwrap();
|
|
|
|
fs.device.write_block(self.id, 0, self.disk_inode.as_buf())?;
|
|
|
|
|
|
|
|
self.disk_inode.sync();
|
|
|
|
self.disk_inode.sync();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fn type_(&self) -> Result<u32, ()> {
|
|
|
|
fn resize(&mut self, len: usize) -> vfs::Result<()> {
|
|
|
|
Ok(self.disk_inode.type_.clone() as u32)
|
|
|
|
if len > MAX_FILE_SIZE {
|
|
|
|
}
|
|
|
|
|
|
|
|
fn resize(&mut self, len: usize) -> Result<(), ()> {
|
|
|
|
|
|
|
|
if self.disk_inode.type_ != FileType::File || len > MAX_FILE_SIZE {
|
|
|
|
|
|
|
|
return Err(());
|
|
|
|
return Err(());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let blocks = ((len + BLKSIZE - 1) / BLKSIZE) as u32;
|
|
|
|
let blocks = ((len + BLKSIZE - 1) / BLKSIZE) as u32;
|
|
|
@ -189,11 +216,18 @@ impl vfs::INode for INode {
|
|
|
|
let fs = self.fs.upgrade().unwrap();
|
|
|
|
let fs = self.fs.upgrade().unwrap();
|
|
|
|
let old_blocks = self.disk_inode.blocks;
|
|
|
|
let old_blocks = self.disk_inode.blocks;
|
|
|
|
self.disk_inode.blocks = blocks;
|
|
|
|
self.disk_inode.blocks = blocks;
|
|
|
|
|
|
|
|
// allocate indirect block if need
|
|
|
|
|
|
|
|
if old_blocks < NDIRECT as u32 && blocks >= NDIRECT as u32 {
|
|
|
|
|
|
|
|
self.disk_inode.indirect = fs.borrow_mut().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.borrow_mut().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
|
|
|
|
|
|
|
|
let old_size = self.disk_inode.size as usize;
|
|
|
|
|
|
|
|
self.clean_at(old_size, len).unwrap();
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Ordering::Less => {
|
|
|
|
Ordering::Less => {
|
|
|
|
let fs = self.fs.upgrade().unwrap();
|
|
|
|
let fs = self.fs.upgrade().unwrap();
|
|
|
@ -202,12 +236,70 @@ impl vfs::INode for INode {
|
|
|
|
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.borrow_mut().free_block(disk_block_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// free indirect block if need
|
|
|
|
|
|
|
|
if blocks < NDIRECT as u32 && self.disk_inode.blocks >= NDIRECT as u32 {
|
|
|
|
|
|
|
|
fs.borrow_mut().free_block(self.disk_inode.indirect as usize);
|
|
|
|
|
|
|
|
self.disk_inode.indirect = 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
self.disk_inode.blocks = blocks;
|
|
|
|
self.disk_inode.blocks = blocks;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.disk_inode.size = len as u32;
|
|
|
|
self.disk_inode.size = len as u32;
|
|
|
|
Ok(())
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn create(&mut self, name: &'static str) -> vfs::Result<Ptr<vfs::INode>> {
|
|
|
|
|
|
|
|
let fs = self.fs.upgrade().unwrap();
|
|
|
|
|
|
|
|
let info = self.info().unwrap();
|
|
|
|
|
|
|
|
assert_eq!(info.type_, vfs::FileType::Dir);
|
|
|
|
|
|
|
|
assert_eq!(info.size % BLKSIZE, 0);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Ensure the name is not exist
|
|
|
|
|
|
|
|
assert!(self.get_file_inode_id(name).is_none(), "file name exist");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Create new INode
|
|
|
|
|
|
|
|
let inode = fs.borrow_mut().new_inode_file().unwrap();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Write new entry
|
|
|
|
|
|
|
|
let entry = DiskEntry {
|
|
|
|
|
|
|
|
id: inode.borrow().id as u32,
|
|
|
|
|
|
|
|
name: Str256::from(name),
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
self.resize(info.size + BLKSIZE).unwrap();
|
|
|
|
|
|
|
|
self.write_at(info.size, entry.as_buf()).unwrap();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Ok(inode)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
fn loopup(&mut self, path: &'static str) -> vfs::Result<Ptr<vfs::INode>> {
|
|
|
|
|
|
|
|
let fs = self.fs.upgrade().unwrap();
|
|
|
|
|
|
|
|
let info = self.info().unwrap();
|
|
|
|
|
|
|
|
assert_eq!(info.type_, vfs::FileType::Dir);
|
|
|
|
|
|
|
|
assert_eq!(info.size % BLKSIZE, 0);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let (name, rest_path) = match path.find('/') {
|
|
|
|
|
|
|
|
None => (path, ""),
|
|
|
|
|
|
|
|
Some(pos) => (&path[0..pos], &path[pos+1..]),
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
let inode_id = self.get_file_inode_id(name);
|
|
|
|
|
|
|
|
if inode_id.is_none() {
|
|
|
|
|
|
|
|
return Err(());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
let inode = fs.borrow_mut().get_inode(inode_id.unwrap());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let type_ = inode.borrow().disk_inode.type_;
|
|
|
|
|
|
|
|
match type_ {
|
|
|
|
|
|
|
|
FileType::File => if rest_path == "" {Ok(inode)} else {Err(())},
|
|
|
|
|
|
|
|
FileType::Dir => inode.borrow_mut().loopup(rest_path),
|
|
|
|
|
|
|
|
_ => unimplemented!(),
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
impl Drop for INode {
|
|
|
|
|
|
|
|
/// Auto sync when drop
|
|
|
|
|
|
|
|
fn drop(&mut self) {
|
|
|
|
|
|
|
|
use vfs::INode;
|
|
|
|
|
|
|
|
self.sync().expect("failed to sync");
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Given a range and iterate sub-range for each block
|
|
|
|
/// Given a range and iterate sub-range for each block
|
|
|
@ -256,11 +348,11 @@ pub struct 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<Ptr<Self>> {
|
|
|
|
let super_block = load_struct::<SuperBlock>(device.as_mut(), 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let free_map = load_struct::<[u8; BLKSIZE]>(device.as_mut(), BLKN_FREEMAP);
|
|
|
|
let free_map = device.load_struct::<[u8; BLKSIZE]>(BLKN_FREEMAP);
|
|
|
|
|
|
|
|
|
|
|
|
Some(SimpleFileSystem {
|
|
|
|
Some(SimpleFileSystem {
|
|
|
|
super_block: Dirty::new(super_block),
|
|
|
|
super_block: Dirty::new(super_block),
|
|
|
@ -279,19 +371,38 @@ impl SimpleFileSystem {
|
|
|
|
magic: MAGIC,
|
|
|
|
magic: MAGIC,
|
|
|
|
blocks: blocks as u32,
|
|
|
|
blocks: blocks as u32,
|
|
|
|
unused_blocks: blocks as u32 - 3,
|
|
|
|
unused_blocks: blocks as u32 - 3,
|
|
|
|
info: Str32::from_slice(b"simple file system"),
|
|
|
|
info: Str32::from("simple file system"),
|
|
|
|
};
|
|
|
|
};
|
|
|
|
let mut free_map = BitSet::with_capacity(BLKBITS);
|
|
|
|
let free_map = {
|
|
|
|
|
|
|
|
let mut bitset = BitSet::with_capacity(BLKBITS);
|
|
|
|
for i in 3 .. blocks {
|
|
|
|
for i in 3 .. blocks {
|
|
|
|
free_map.insert(i);
|
|
|
|
bitset.insert(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
SimpleFileSystem {
|
|
|
|
bitset
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let sfs = SimpleFileSystem {
|
|
|
|
super_block: Dirty::new_dirty(super_block),
|
|
|
|
super_block: Dirty::new_dirty(super_block),
|
|
|
|
free_map: Dirty::new_dirty(free_map),
|
|
|
|
free_map: Dirty::new_dirty(free_map),
|
|
|
|
inodes: BTreeMap::<INodeId, Ptr<INode>>::new(),
|
|
|
|
inodes: BTreeMap::<INodeId, Ptr<INode>>::new(),
|
|
|
|
device,
|
|
|
|
device,
|
|
|
|
self_ptr: Weak::default(),
|
|
|
|
self_ptr: Weak::default(),
|
|
|
|
}.wrap()
|
|
|
|
}.wrap();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Init root INode
|
|
|
|
|
|
|
|
let inode = Rc::new(RefCell::new(INode {
|
|
|
|
|
|
|
|
disk_inode: Dirty::new_dirty(DiskINode::new_dir()),
|
|
|
|
|
|
|
|
id: BLKN_ROOT,
|
|
|
|
|
|
|
|
fs: Rc::downgrade(&sfs),
|
|
|
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
inode.borrow_mut().init_dir(BLKN_ROOT).unwrap();
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
use vfs::INode;
|
|
|
|
|
|
|
|
inode.borrow_mut().sync().unwrap();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
sfs.borrow_mut().inodes.insert(BLKN_ROOT, inode);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sfs
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/// Wrap pure SimpleFileSystem with Rc<RefCell<...>>
|
|
|
|
/// Wrap pure SimpleFileSystem with Rc<RefCell<...>>
|
|
|
|
/// Used in constructors
|
|
|
|
/// Used in constructors
|
|
|
@ -322,7 +433,7 @@ impl SimpleFileSystem {
|
|
|
|
|
|
|
|
|
|
|
|
// Load if not in memory.
|
|
|
|
// Load if not in memory.
|
|
|
|
if !self.inodes.contains_key(&id) {
|
|
|
|
if !self.inodes.contains_key(&id) {
|
|
|
|
let disk_inode = load_struct::<DiskINode>(self.device.as_mut(), id);
|
|
|
|
let disk_inode = self.device.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,
|
|
|
@ -334,17 +445,39 @@ impl SimpleFileSystem {
|
|
|
|
self.inodes.get(&id).unwrap().clone()
|
|
|
|
self.inodes.get(&id).unwrap().clone()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Create a new INode file
|
|
|
|
|
|
|
|
fn new_inode_file(&mut self) -> vfs::Result<Ptr<INode>> {
|
|
|
|
|
|
|
|
let id = self.alloc_block().unwrap();
|
|
|
|
|
|
|
|
Ok(Rc::new(RefCell::new(INode {
|
|
|
|
|
|
|
|
disk_inode: Dirty::new_dirty(DiskINode::new_file()),
|
|
|
|
|
|
|
|
id,
|
|
|
|
|
|
|
|
fs: self.self_ptr.clone(),
|
|
|
|
|
|
|
|
})))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Create a new INode dir
|
|
|
|
|
|
|
|
fn new_inode_dir(&mut self, parent: INodeId) -> vfs::Result<Ptr<INode>> {
|
|
|
|
|
|
|
|
let id = self.alloc_block().unwrap();
|
|
|
|
|
|
|
|
let mut inode = INode {
|
|
|
|
|
|
|
|
disk_inode: Dirty::new_dirty(DiskINode::new_dir()),
|
|
|
|
|
|
|
|
id,
|
|
|
|
|
|
|
|
fs: self.self_ptr.clone(),
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
inode.init_dir(parent).unwrap();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Ok(Rc::new(RefCell::new(inode)))
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl vfs::FileSystem for SimpleFileSystem {
|
|
|
|
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) -> Result<(), ()> {
|
|
|
|
fn sync(&mut self) -> vfs::Result<()> {
|
|
|
|
let SimpleFileSystem {
|
|
|
|
let SimpleFileSystem {
|
|
|
|
ref mut super_block,
|
|
|
|
ref mut super_block,
|
|
|
|
ref mut device,
|
|
|
|
ref mut device,
|
|
|
|
ref mut free_map,
|
|
|
|
ref mut free_map,
|
|
|
|
|
|
|
|
ref mut inodes,
|
|
|
|
..
|
|
|
|
..
|
|
|
|
} = self;
|
|
|
|
} = self;
|
|
|
|
|
|
|
|
|
|
|
@ -356,6 +489,10 @@ impl vfs::FileSystem for SimpleFileSystem {
|
|
|
|
device.write_at(BLKSIZE * BLKN_FREEMAP, free_map.as_buf()).unwrap();
|
|
|
|
device.write_at(BLKSIZE * BLKN_FREEMAP, free_map.as_buf()).unwrap();
|
|
|
|
free_map.sync();
|
|
|
|
free_map.sync();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for inode in inodes.values() {
|
|
|
|
|
|
|
|
use vfs::INode;
|
|
|
|
|
|
|
|
inode.borrow_mut().sync().unwrap();
|
|
|
|
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -363,7 +500,7 @@ impl vfs::FileSystem for SimpleFileSystem {
|
|
|
|
self.get_inode(BLKN_ROOT)
|
|
|
|
self.get_inode(BLKN_ROOT)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn unmount(&mut self) -> Result<(), ()> {
|
|
|
|
fn unmount(&mut self) -> vfs::Result<()> {
|
|
|
|
unimplemented!()
|
|
|
|
unimplemented!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -408,6 +545,16 @@ impl AsBuf for BitSet {
|
|
|
|
|
|
|
|
|
|
|
|
impl AsBuf for [u8; BLKSIZE] {}
|
|
|
|
impl AsBuf for [u8; BLKSIZE] {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
impl From<FileType> for vfs::FileType {
|
|
|
|
|
|
|
|
fn from(t: FileType) -> Self {
|
|
|
|
|
|
|
|
match t {
|
|
|
|
|
|
|
|
FileType::File => vfs::FileType::File,
|
|
|
|
|
|
|
|
FileType::Dir => vfs::FileType::Dir,
|
|
|
|
|
|
|
|
_ => panic!("unknown file type"),
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
mod test {
|
|
|
|
use super::*;
|
|
|
|
use super::*;
|
|
|
|