|
|
@ -1,16 +1,35 @@
|
|
|
|
use spin::Mutex;
|
|
|
|
use spin::Mutex;
|
|
|
|
use bit_set::BitSet;
|
|
|
|
use bit_set::BitSet;
|
|
|
|
use alloc::{boxed::Box, Vec, BTreeMap, rc::Rc};
|
|
|
|
use alloc::{boxed::Box, Vec, BTreeMap, rc::{Rc, Weak}};
|
|
|
|
|
|
|
|
use core::cell::{RefCell, RefMut};
|
|
|
|
use dirty::Dirty;
|
|
|
|
use dirty::Dirty;
|
|
|
|
use super::structs::*;
|
|
|
|
use super::structs::*;
|
|
|
|
|
|
|
|
use super::vfs;
|
|
|
|
use core::mem::{uninitialized, size_of};
|
|
|
|
use core::mem::{uninitialized, size_of};
|
|
|
|
use core::slice;
|
|
|
|
use core::slice;
|
|
|
|
|
|
|
|
|
|
|
|
/// Interface for SFS to read & write
|
|
|
|
/// Interface for SFS to read & write
|
|
|
|
/// TODO: use std::io::{Read, Write}
|
|
|
|
/// TODO: use std::io::{Read, Write}
|
|
|
|
pub trait Device {
|
|
|
|
pub trait Device {
|
|
|
|
fn read_at(&mut self, offset: u64, buf: &mut [u8]) -> Option<usize>;
|
|
|
|
fn read_at(&mut self, offset: usize, buf: &mut [u8]) -> Option<usize>;
|
|
|
|
fn write_at(&mut self, offset: u64, buf: &[u8]) -> Option<usize>;
|
|
|
|
fn write_at(&mut self, offset: usize, buf: &[u8]) -> Option<usize>;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Helper functions
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn read_block(&mut self, id: BlockId, offset: usize, buf: &mut [u8]) -> Result<(),()> {
|
|
|
|
|
|
|
|
debug_assert!(offset + buf.len() <= BLKSIZE);
|
|
|
|
|
|
|
|
match self.read_at(id * BLKSIZE + offset, buf) {
|
|
|
|
|
|
|
|
Some(len) if len == buf.len() => Ok(()),
|
|
|
|
|
|
|
|
_ => Err(()),
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
fn write_block(&mut self, id: BlockId, offset: usize, buf: &[u8]) -> Result<(),()> {
|
|
|
|
|
|
|
|
debug_assert!(offset + buf.len() <= BLKSIZE);
|
|
|
|
|
|
|
|
match self.write_at(id * BLKSIZE + offset, buf) {
|
|
|
|
|
|
|
|
Some(len) if len == buf.len() => Ok(()),
|
|
|
|
|
|
|
|
_ => Err(()),
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// inode for sfs
|
|
|
|
/// inode for sfs
|
|
|
@ -19,9 +38,121 @@ pub struct INode {
|
|
|
|
disk_inode: Dirty<DiskINode>,
|
|
|
|
disk_inode: Dirty<DiskINode>,
|
|
|
|
/// inode number
|
|
|
|
/// inode number
|
|
|
|
id: INodeId,
|
|
|
|
id: INodeId,
|
|
|
|
|
|
|
|
/// Reference to SFS, used by almost all operations
|
|
|
|
|
|
|
|
fs: Rc<RefCell<SimpleFileSystem>>,
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
impl INode {
|
|
|
|
|
|
|
|
/// Map file block id to disk block id
|
|
|
|
|
|
|
|
fn disk_block_id(&self, file_block_id: BlockId) -> Option<BlockId> {
|
|
|
|
|
|
|
|
match file_block_id {
|
|
|
|
|
|
|
|
id if id >= self.disk_inode.blocks as BlockId =>
|
|
|
|
|
|
|
|
None,
|
|
|
|
|
|
|
|
id if id < NDIRECT =>
|
|
|
|
|
|
|
|
Some(self.disk_inode.direct[id] as BlockId),
|
|
|
|
|
|
|
|
id if id < NDIRECT + BLK_NENTRY => {
|
|
|
|
|
|
|
|
let mut disk_block_id: BlockId = 0;
|
|
|
|
|
|
|
|
self.fs.borrow_mut().device.read_block(
|
|
|
|
|
|
|
|
self.disk_inode.indirect as usize,
|
|
|
|
|
|
|
|
ENTRY_SIZE * (id - NDIRECT),
|
|
|
|
|
|
|
|
disk_block_id.as_buf_mut()
|
|
|
|
|
|
|
|
).unwrap();
|
|
|
|
|
|
|
|
Some(disk_block_id as BlockId)
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
id => unimplemented!("double indirect blocks is not supported"),
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
impl vfs::INode for INode {
|
|
|
|
|
|
|
|
fn open(&mut self, flags: u32) -> Result<(), ()> {
|
|
|
|
|
|
|
|
// Do nothing
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
fn close(&mut self) -> Result<(), ()> {
|
|
|
|
|
|
|
|
self.sync()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
fn read_at(&mut self, offset: usize, buf: &mut [u8]) -> Option<usize> {
|
|
|
|
|
|
|
|
let mut fs = self.fs.borrow_mut();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let iter = BlockIter {
|
|
|
|
|
|
|
|
begin: offset,
|
|
|
|
|
|
|
|
end: offset + buf.len(),
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Read for each block
|
|
|
|
|
|
|
|
let mut buf_offset = 0usize;
|
|
|
|
|
|
|
|
for BlockRange{block, begin, end} in iter {
|
|
|
|
|
|
|
|
if let Some(disk_block_id) = self.disk_block_id(block) {
|
|
|
|
|
|
|
|
let len = end - begin;
|
|
|
|
|
|
|
|
fs.device.read_block(disk_block_id, begin, &mut buf[buf_offset .. buf_offset + len]);
|
|
|
|
|
|
|
|
buf_offset += len;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
// Failed this time
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
Some(buf_offset)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
fn write_at(&mut self, offset: usize, buf: &[u8]) -> Option<usize> {
|
|
|
|
|
|
|
|
let mut fs = self.fs.borrow_mut();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let iter = BlockIter {
|
|
|
|
|
|
|
|
begin: offset,
|
|
|
|
|
|
|
|
end: offset + buf.len(),
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Read for each block
|
|
|
|
|
|
|
|
let mut buf_offset = 0usize;
|
|
|
|
|
|
|
|
for BlockRange{block, begin, end} in iter {
|
|
|
|
|
|
|
|
if let Some(disk_block_id) = self.disk_block_id(block) {
|
|
|
|
|
|
|
|
let len = end - begin;
|
|
|
|
|
|
|
|
fs.device.write_block(disk_block_id, begin, &buf[buf_offset .. buf_offset + len]);
|
|
|
|
|
|
|
|
buf_offset += len;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
// Failed this time
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
Some(buf_offset)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
fn sync(&mut self) -> Result<(), ()> {
|
|
|
|
|
|
|
|
if self.disk_inode.dirty() {
|
|
|
|
|
|
|
|
let mut fs = self.fs.borrow_mut();
|
|
|
|
|
|
|
|
fs.device.write_block(self.id, 0, self.disk_inode.as_buf())?;
|
|
|
|
|
|
|
|
self.disk_inode.sync();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Given a range and iterate sub-range for each block
|
|
|
|
|
|
|
|
struct BlockIter {
|
|
|
|
|
|
|
|
begin: usize,
|
|
|
|
|
|
|
|
end: usize,
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
struct BlockRange {
|
|
|
|
|
|
|
|
block: BlockId,
|
|
|
|
|
|
|
|
begin: usize,
|
|
|
|
|
|
|
|
end: usize,
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
impl Iterator for BlockIter {
|
|
|
|
|
|
|
|
type Item = BlockRange;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<<Self as Iterator>::Item> {
|
|
|
|
|
|
|
|
if self.begin >= self.end {
|
|
|
|
|
|
|
|
return None;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
let block = self.begin / BLKSIZE;
|
|
|
|
|
|
|
|
let begin = self.begin % BLKSIZE;
|
|
|
|
|
|
|
|
let end = if block == self.end / BLKSIZE {self.end % BLKSIZE} else {BLKSIZE};
|
|
|
|
|
|
|
|
self.begin += end - begin;
|
|
|
|
|
|
|
|
Some(BlockRange {block, begin, end})
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type INodeId = usize;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// filesystem for sfs
|
|
|
|
/// filesystem for sfs
|
|
|
|
pub struct SimpleFileSystem {
|
|
|
|
pub struct SimpleFileSystem {
|
|
|
@ -39,7 +170,7 @@ impl SimpleFileSystem {
|
|
|
|
/// Create a new SFS with device
|
|
|
|
/// Create a new SFS with device
|
|
|
|
pub fn new(mut device: Box<Device>) -> Option<Self> {
|
|
|
|
pub fn new(mut device: Box<Device>) -> Option<Self> {
|
|
|
|
let mut super_block: SuperBlock = unsafe{ uninitialized() };
|
|
|
|
let mut super_block: SuperBlock = unsafe{ uninitialized() };
|
|
|
|
if device.read_at(0, super_block.as_buf_mut()).is_none() {
|
|
|
|
if device.read_at(BLKN_SUPER * BLKSIZE, super_block.as_buf_mut()).is_none() {
|
|
|
|
return None;
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if super_block.check() == false {
|
|
|
|
if super_block.check() == false {
|
|
|
@ -86,6 +217,26 @@ impl SimpleFileSystem {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
impl vfs::FileSystem for SimpleFileSystem {
|
|
|
|
|
|
|
|
type INode = INode;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn sync(&mut self) -> Result<(), ()> {
|
|
|
|
|
|
|
|
unimplemented!()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn root_inode(&mut self) -> Rc<INode> {
|
|
|
|
|
|
|
|
unimplemented!()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn unmount(&mut self) -> Result<(), ()> {
|
|
|
|
|
|
|
|
unimplemented!()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn cleanup(&mut self) {
|
|
|
|
|
|
|
|
unimplemented!()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
trait BitsetAlloc {
|
|
|
|
trait BitsetAlloc {
|
|
|
|
fn alloc(&mut self) -> Option<usize>;
|
|
|
|
fn alloc(&mut self) -> Option<usize>;
|
|
|
|
}
|
|
|
|
}
|
|
|
|