|
|
@ -18,7 +18,7 @@ use bitvec::BitVec;
|
|
|
|
use rcore_fs::dev::TimeProvider;
|
|
|
|
use rcore_fs::dev::TimeProvider;
|
|
|
|
use rcore_fs::dirty::Dirty;
|
|
|
|
use rcore_fs::dirty::Dirty;
|
|
|
|
use rcore_fs::vfs::{self, FileSystem, FsError, INode, Timespec};
|
|
|
|
use rcore_fs::vfs::{self, FileSystem, FsError, INode, Timespec};
|
|
|
|
//use log::*;
|
|
|
|
use log::*;
|
|
|
|
use spin::RwLock;
|
|
|
|
use spin::RwLock;
|
|
|
|
|
|
|
|
|
|
|
|
use self::dev::*;
|
|
|
|
use self::dev::*;
|
|
|
@ -143,14 +143,16 @@ impl INodeImpl {
|
|
|
|
|
|
|
|
|
|
|
|
impl vfs::INode for INodeImpl {
|
|
|
|
impl vfs::INode for INodeImpl {
|
|
|
|
fn read_at(&self, offset: usize, buf: &mut [u8]) -> vfs::Result<usize> {
|
|
|
|
fn read_at(&self, offset: usize, buf: &mut [u8]) -> vfs::Result<usize> {
|
|
|
|
if self.disk_inode.read().type_ != FileType::File {
|
|
|
|
let type_ = self.disk_inode.read().type_;
|
|
|
|
|
|
|
|
if type_ != FileType::File && type_ != FileType::SymLink {
|
|
|
|
return Err(FsError::NotFile);
|
|
|
|
return Err(FsError::NotFile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let len = self.file.read_at(buf, offset)?;
|
|
|
|
let len = self.file.read_at(buf, offset)?;
|
|
|
|
Ok(len)
|
|
|
|
Ok(len)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fn write_at(&self, offset: usize, buf: &[u8]) -> vfs::Result<usize> {
|
|
|
|
fn write_at(&self, offset: usize, buf: &[u8]) -> vfs::Result<usize> {
|
|
|
|
if self.disk_inode.read().type_ != FileType::File {
|
|
|
|
let type_ = self.disk_inode.read().type_;
|
|
|
|
|
|
|
|
if type_ != FileType::File && type_ != FileType::SymLink {
|
|
|
|
return Err(FsError::NotFile);
|
|
|
|
return Err(FsError::NotFile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let len = self.file.write_at(buf, offset)?;
|
|
|
|
let len = self.file.write_at(buf, offset)?;
|
|
|
@ -170,7 +172,7 @@ impl vfs::INode for INodeImpl {
|
|
|
|
dev: 0,
|
|
|
|
dev: 0,
|
|
|
|
inode: self.id,
|
|
|
|
inode: self.id,
|
|
|
|
size: match disk_inode.type_ {
|
|
|
|
size: match disk_inode.type_ {
|
|
|
|
FileType::File => disk_inode.size as usize,
|
|
|
|
FileType::File | FileType::SymLink => disk_inode.size as usize,
|
|
|
|
FileType::Dir => disk_inode.blocks as usize,
|
|
|
|
FileType::Dir => disk_inode.blocks as usize,
|
|
|
|
_ => panic!("Unknown file type"),
|
|
|
|
_ => panic!("Unknown file type"),
|
|
|
|
},
|
|
|
|
},
|
|
|
@ -221,7 +223,8 @@ impl vfs::INode for INodeImpl {
|
|
|
|
Ok(())
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fn resize(&self, len: usize) -> vfs::Result<()> {
|
|
|
|
fn resize(&self, len: usize) -> vfs::Result<()> {
|
|
|
|
if self.disk_inode.read().type_ != FileType::File {
|
|
|
|
let type_ = self.disk_inode.read().type_;
|
|
|
|
|
|
|
|
if type_ != FileType::File && type_ != FileType::SymLink {
|
|
|
|
return Err(FsError::NotFile);
|
|
|
|
return Err(FsError::NotFile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.file.set_len(len)?;
|
|
|
|
self.file.set_len(len)?;
|
|
|
@ -232,6 +235,7 @@ impl vfs::INode for INodeImpl {
|
|
|
|
let type_ = match type_ {
|
|
|
|
let type_ = match type_ {
|
|
|
|
vfs::FileType::File => FileType::File,
|
|
|
|
vfs::FileType::File => FileType::File,
|
|
|
|
vfs::FileType::Dir => FileType::Dir,
|
|
|
|
vfs::FileType::Dir => FileType::Dir,
|
|
|
|
|
|
|
|
vfs::FileType::SymLink => FileType::SymLink,
|
|
|
|
_ => return Err(vfs::FsError::InvalidParam),
|
|
|
|
_ => return Err(vfs::FsError::InvalidParam),
|
|
|
|
};
|
|
|
|
};
|
|
|
|
let info = self.metadata()?;
|
|
|
|
let info = self.metadata()?;
|
|
|
@ -464,11 +468,18 @@ impl SEFS {
|
|
|
|
if !super_block.check() {
|
|
|
|
if !super_block.check() {
|
|
|
|
return Err(FsError::WrongFs);
|
|
|
|
return Err(FsError::WrongFs);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let free_map = meta_file.load_struct::<[u8; BLKSIZE]>(BLKN_FREEMAP)?;
|
|
|
|
|
|
|
|
|
|
|
|
// load free map
|
|
|
|
|
|
|
|
let mut free_map = BitVec::with_capacity(BLKBITS * super_block.groups as usize);
|
|
|
|
|
|
|
|
unsafe { free_map.set_len(BLKBITS * super_block.groups as usize); }
|
|
|
|
|
|
|
|
for i in 0..super_block.groups as usize {
|
|
|
|
|
|
|
|
let block_id = Self::get_freemap_block_id_of_group(i);
|
|
|
|
|
|
|
|
meta_file.read_block(block_id, &mut free_map.as_mut()[BLKSIZE * i..BLKSIZE * (i+1)])?;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Ok(SEFS {
|
|
|
|
Ok(SEFS {
|
|
|
|
super_block: RwLock::new(Dirty::new(super_block)),
|
|
|
|
super_block: RwLock::new(Dirty::new(super_block)),
|
|
|
|
free_map: RwLock::new(Dirty::new(BitVec::from(free_map.as_ref()))),
|
|
|
|
free_map: RwLock::new(Dirty::new(free_map)),
|
|
|
|
inodes: RwLock::new(BTreeMap::new()),
|
|
|
|
inodes: RwLock::new(BTreeMap::new()),
|
|
|
|
device,
|
|
|
|
device,
|
|
|
|
meta_file,
|
|
|
|
meta_file,
|
|
|
@ -488,6 +499,7 @@ impl SEFS {
|
|
|
|
magic: MAGIC,
|
|
|
|
magic: MAGIC,
|
|
|
|
blocks: blocks as u32,
|
|
|
|
blocks: blocks as u32,
|
|
|
|
unused_blocks: blocks as u32 - 2,
|
|
|
|
unused_blocks: blocks as u32 - 2,
|
|
|
|
|
|
|
|
groups: 1,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
let free_map = {
|
|
|
|
let free_map = {
|
|
|
|
let mut bitset = BitVec::with_capacity(BLKBITS);
|
|
|
|
let mut bitset = BitVec::with_capacity(BLKBITS);
|
|
|
@ -538,15 +550,22 @@ impl SEFS {
|
|
|
|
/// Allocate a block, return block id
|
|
|
|
/// Allocate a block, return block id
|
|
|
|
fn alloc_block(&self) -> Option<usize> {
|
|
|
|
fn alloc_block(&self) -> Option<usize> {
|
|
|
|
let mut free_map = self.free_map.write();
|
|
|
|
let mut free_map = self.free_map.write();
|
|
|
|
let id = free_map.alloc();
|
|
|
|
let mut super_block = self.super_block.write();
|
|
|
|
if let Some(block_id) = id {
|
|
|
|
let id = free_map.alloc().or_else(|| {
|
|
|
|
let mut super_block = self.super_block.write();
|
|
|
|
// allocate a new group
|
|
|
|
if super_block.unused_blocks == 0 {
|
|
|
|
let new_group_id = super_block.groups as usize;
|
|
|
|
free_map.set(block_id, true);
|
|
|
|
super_block.groups += 1;
|
|
|
|
return None;
|
|
|
|
super_block.blocks += BLKBITS as u32;
|
|
|
|
}
|
|
|
|
super_block.unused_blocks += BLKBITS as u32 - 1;
|
|
|
|
super_block.unused_blocks -= 1; // will not underflow
|
|
|
|
self.meta_file.set_len(super_block.groups as usize * BLKBITS * BLKSIZE)
|
|
|
|
}
|
|
|
|
.expect("failed to extend meta file");
|
|
|
|
|
|
|
|
free_map.extend(core::iter::repeat(true).take(BLKBITS));
|
|
|
|
|
|
|
|
free_map.set(Self::get_freemap_block_id_of_group(new_group_id), false);
|
|
|
|
|
|
|
|
// allocate block again
|
|
|
|
|
|
|
|
free_map.alloc()
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
assert!(id.is_some(), "allocate block should always success");
|
|
|
|
|
|
|
|
super_block.unused_blocks -= 1;
|
|
|
|
id
|
|
|
|
id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/// Free a block
|
|
|
|
/// Free a block
|
|
|
@ -621,29 +640,39 @@ impl SEFS {
|
|
|
|
inodes.remove(&id);
|
|
|
|
inodes.remove(&id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_freemap_block_id_of_group(group_id: usize) -> usize {
|
|
|
|
|
|
|
|
BLKBITS * group_id + BLKN_FREEMAP
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl vfs::FileSystem for SEFS {
|
|
|
|
impl vfs::FileSystem for SEFS {
|
|
|
|
/// Write back super block if dirty
|
|
|
|
/// Write back super block if dirty
|
|
|
|
fn sync(&self) -> vfs::Result<()> {
|
|
|
|
fn sync(&self) -> vfs::Result<()> {
|
|
|
|
|
|
|
|
// sync super_block
|
|
|
|
let mut super_block = self.super_block.write();
|
|
|
|
let mut super_block = self.super_block.write();
|
|
|
|
if super_block.dirty() {
|
|
|
|
if super_block.dirty() {
|
|
|
|
self.meta_file
|
|
|
|
self.meta_file
|
|
|
|
.write_all_at(super_block.as_buf(), BLKSIZE * BLKN_SUPER)?;
|
|
|
|
.write_all_at(super_block.as_buf(), BLKSIZE * BLKN_SUPER)?;
|
|
|
|
super_block.sync();
|
|
|
|
super_block.sync();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// sync free_map
|
|
|
|
let mut free_map = self.free_map.write();
|
|
|
|
let mut free_map = self.free_map.write();
|
|
|
|
if free_map.dirty() {
|
|
|
|
if free_map.dirty() {
|
|
|
|
self.meta_file
|
|
|
|
for i in 0..super_block.groups as usize {
|
|
|
|
.write_all_at(free_map.as_buf(), BLKSIZE * BLKN_FREEMAP)?;
|
|
|
|
let slice = &free_map.as_ref()[BLKSIZE * i..BLKSIZE * (i+1)];
|
|
|
|
|
|
|
|
self.meta_file
|
|
|
|
|
|
|
|
.write_all_at(slice, BLKSIZE * Self::get_freemap_block_id_of_group(i))?;
|
|
|
|
|
|
|
|
}
|
|
|
|
free_map.sync();
|
|
|
|
free_map.sync();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// sync all INodes
|
|
|
|
self.flush_weak_inodes();
|
|
|
|
self.flush_weak_inodes();
|
|
|
|
for inode in self.inodes.read().values() {
|
|
|
|
for inode in self.inodes.read().values() {
|
|
|
|
if let Some(inode) = inode.upgrade() {
|
|
|
|
if let Some(inode) = inode.upgrade() {
|
|
|
|
inode.sync_all()?;
|
|
|
|
inode.sync_all()?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
self.meta_file.flush()?;
|
|
|
|
Ok(())
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -670,7 +699,7 @@ impl Drop for SEFS {
|
|
|
|
/// Auto sync when drop
|
|
|
|
/// Auto sync when drop
|
|
|
|
fn drop(&mut self) {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
self.sync()
|
|
|
|
self.sync()
|
|
|
|
.expect("Failed to sync when dropping the SimpleFileSystem");
|
|
|
|
.expect("Failed to sync when dropping the SEFS");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -705,6 +734,7 @@ impl From<FileType> for vfs::FileType {
|
|
|
|
match t {
|
|
|
|
match t {
|
|
|
|
FileType::File => vfs::FileType::File,
|
|
|
|
FileType::File => vfs::FileType::File,
|
|
|
|
FileType::Dir => vfs::FileType::Dir,
|
|
|
|
FileType::Dir => vfs::FileType::Dir,
|
|
|
|
|
|
|
|
FileType::SymLink => vfs::FileType::SymLink,
|
|
|
|
_ => panic!("unknown file type"),
|
|
|
|
_ => panic!("unknown file type"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|