|
|
@ -1,28 +1,28 @@
|
|
|
|
use bit_vec::BitVec;
|
|
|
|
use bit_vec::BitVec;
|
|
|
|
use alloc::{boxed::Box, vec::Vec, collections::BTreeMap, rc::{Rc, Weak}, string::String};
|
|
|
|
use alloc::{boxed::Box, vec::Vec, collections::BTreeMap, sync::{Arc, Weak}, string::String};
|
|
|
|
use core::cell::{RefCell, RefMut};
|
|
|
|
|
|
|
|
use core::mem::{uninitialized, size_of};
|
|
|
|
use core::mem::{uninitialized, size_of};
|
|
|
|
use core::slice;
|
|
|
|
use core::slice;
|
|
|
|
use core::fmt::{Debug, Formatter, Error};
|
|
|
|
use core::fmt::{Debug, Formatter, Error};
|
|
|
|
use core::any::Any;
|
|
|
|
use core::any::Any;
|
|
|
|
use dirty::Dirty;
|
|
|
|
use dirty::Dirty;
|
|
|
|
use structs::*;
|
|
|
|
use structs::*;
|
|
|
|
use vfs::{self, Device};
|
|
|
|
use vfs::{self, Device, INode, FileSystem};
|
|
|
|
use util::*;
|
|
|
|
use util::*;
|
|
|
|
|
|
|
|
use spin::{Mutex, RwLock};
|
|
|
|
|
|
|
|
|
|
|
|
impl Device {
|
|
|
|
impl Device {
|
|
|
|
fn read_block(&mut self, id: BlockId, offset: usize, buf: &mut [u8]) -> vfs::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(()),
|
|
|
|
_ => panic!(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fn write_block(&mut self, id: BlockId, offset: usize, buf: &[u8]) -> vfs::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(()),
|
|
|
|
_ => Err(()),
|
|
|
|
_ => panic!(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/// Load struct `T` from given block in device
|
|
|
|
/// Load struct `T` from given block in device
|
|
|
@ -33,61 +33,58 @@ impl Device {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type Ptr<T> = Rc<RefCell<T>>;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// inode for sfs
|
|
|
|
/// inode for sfs
|
|
|
|
pub struct INode {
|
|
|
|
pub struct INodeImpl {
|
|
|
|
/// on-disk inode
|
|
|
|
|
|
|
|
disk_inode: Dirty<DiskINode>,
|
|
|
|
|
|
|
|
/// inode number
|
|
|
|
/// inode number
|
|
|
|
id: INodeId,
|
|
|
|
id: INodeId,
|
|
|
|
|
|
|
|
/// on-disk inode
|
|
|
|
|
|
|
|
disk_inode: RwLock<Dirty<DiskINode>>,
|
|
|
|
/// Weak reference to SFS, used by almost all operations
|
|
|
|
/// Weak reference to SFS, used by almost all operations
|
|
|
|
fs: Weak<SimpleFileSystem>,
|
|
|
|
fs: Arc<SimpleFileSystem>,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl Debug for INode {
|
|
|
|
impl Debug for INodeImpl {
|
|
|
|
fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
|
|
|
|
fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
|
|
|
|
write!(f, "INode {{ id: {}, disk: {:?} }}", self.id, self.disk_inode)
|
|
|
|
write!(f, "INode {{ id: {}, disk: {:?} }}", self.id, self.disk_inode)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl INode {
|
|
|
|
impl INodeImpl {
|
|
|
|
/// Map file block id to disk block id
|
|
|
|
/// Map file block id to disk block id
|
|
|
|
fn get_disk_block_id(&self, file_block_id: BlockId) -> Option<BlockId> {
|
|
|
|
fn get_disk_block_id(&self, file_block_id: BlockId) -> vfs::Result<BlockId> {
|
|
|
|
|
|
|
|
let disk_inode = self.disk_inode.read();
|
|
|
|
match file_block_id {
|
|
|
|
match file_block_id {
|
|
|
|
id if id >= self.disk_inode.blocks as BlockId =>
|
|
|
|
id if id >= disk_inode.blocks as BlockId =>
|
|
|
|
None,
|
|
|
|
panic!(),
|
|
|
|
id if id < NDIRECT =>
|
|
|
|
id if id < NDIRECT =>
|
|
|
|
Some(self.disk_inode.direct[id] as BlockId),
|
|
|
|
Ok(disk_inode.direct[id] as BlockId),
|
|
|
|
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();
|
|
|
|
self.fs.device.lock().read_block(
|
|
|
|
fs.device.borrow_mut().read_block(
|
|
|
|
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(),
|
|
|
|
).unwrap();
|
|
|
|
)?;
|
|
|
|
Some(disk_block_id as BlockId)
|
|
|
|
Ok(disk_block_id as BlockId)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => unimplemented!("double indirect blocks is not supported"),
|
|
|
|
_ => unimplemented!("double indirect blocks is not supported"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fn set_disk_block_id(&mut self, file_block_id: BlockId, disk_block_id: BlockId) -> vfs::Result<()> {
|
|
|
|
fn set_disk_block_id(&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.read().blocks as BlockId =>
|
|
|
|
Err(()),
|
|
|
|
panic!(),
|
|
|
|
id if id < NDIRECT => {
|
|
|
|
id if id < NDIRECT => {
|
|
|
|
self.disk_inode.direct[id] = disk_block_id as u32;
|
|
|
|
self.disk_inode.write().direct[id] = disk_block_id as u32;
|
|
|
|
Ok(())
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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();
|
|
|
|
self.fs.device.lock().write_block(
|
|
|
|
fs.device.borrow_mut().write_block(
|
|
|
|
self.disk_inode.read().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(),
|
|
|
|
).unwrap();
|
|
|
|
)?;
|
|
|
|
Ok(())
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => unimplemented!("double indirect blocks is not supported"),
|
|
|
|
_ => unimplemented!("double indirect blocks is not supported"),
|
|
|
@ -95,9 +92,8 @@ impl INode {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/// Only for Dir
|
|
|
|
/// Only for Dir
|
|
|
|
fn get_file_inode_and_entry_id(&self, name: &str) -> Option<(INodeId, usize)> {
|
|
|
|
fn get_file_inode_and_entry_id(&self, name: &str) -> Option<(INodeId, usize)> {
|
|
|
|
(0..self.disk_inode.blocks)
|
|
|
|
(0..self.disk_inode.read().blocks)
|
|
|
|
.map(|i| {
|
|
|
|
.map(|i| {
|
|
|
|
use vfs::INode;
|
|
|
|
|
|
|
|
let mut entry: DiskEntry = unsafe { uninitialized() };
|
|
|
|
let mut entry: DiskEntry = unsafe { uninitialized() };
|
|
|
|
self._read_at(i as usize * BLKSIZE, entry.as_buf_mut()).unwrap();
|
|
|
|
self._read_at(i as usize * BLKSIZE, entry.as_buf_mut()).unwrap();
|
|
|
|
(entry, i)
|
|
|
|
(entry, i)
|
|
|
@ -110,54 +106,53 @@ impl INode {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/// Init dir content. Insert 2 init entries.
|
|
|
|
/// Init dir content. Insert 2 init entries.
|
|
|
|
/// This do not init nlinks, please modify the nlinks in the invoker.
|
|
|
|
/// This do not init nlinks, please modify the nlinks in the invoker.
|
|
|
|
fn init_dir_entry(&mut self, parent: INodeId) -> vfs::Result<()> {
|
|
|
|
fn init_dir_entry(&self, parent: INodeId) -> vfs::Result<()> {
|
|
|
|
use vfs::INode;
|
|
|
|
|
|
|
|
let fs = self.fs.upgrade().unwrap();
|
|
|
|
|
|
|
|
// Insert entries: '.' '..'
|
|
|
|
// Insert entries: '.' '..'
|
|
|
|
self._resize(BLKSIZE * 2).unwrap();
|
|
|
|
self._resize(BLKSIZE * 2)?;
|
|
|
|
self._write_at(BLKSIZE * 1, DiskEntry {
|
|
|
|
self._write_at(BLKSIZE * 1, DiskEntry {
|
|
|
|
id: parent as u32,
|
|
|
|
id: parent as u32,
|
|
|
|
name: Str256::from(".."),
|
|
|
|
name: Str256::from(".."),
|
|
|
|
}.as_buf()).unwrap();
|
|
|
|
}.as_buf())?;
|
|
|
|
self._write_at(BLKSIZE * 0, DiskEntry {
|
|
|
|
self._write_at(BLKSIZE * 0, DiskEntry {
|
|
|
|
id: self.id as u32,
|
|
|
|
id: self.id as u32,
|
|
|
|
name: Str256::from("."),
|
|
|
|
name: Str256::from("."),
|
|
|
|
}.as_buf()).unwrap();
|
|
|
|
}.as_buf())?;
|
|
|
|
Ok(())
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/// remove a page in middle of file and insert the last page here, useful for dirent remove
|
|
|
|
/// remove a page in middle of file and insert the last page here, useful for dirent remove
|
|
|
|
/// should be only used in unlink
|
|
|
|
/// should be only used in unlink
|
|
|
|
fn remove_dirent_page(&mut self, id: usize) -> vfs::Result<()> {
|
|
|
|
fn remove_dirent_page(&self, id: usize) -> vfs::Result<()> {
|
|
|
|
assert!(id < self.disk_inode.blocks as usize);
|
|
|
|
assert!(id < self.disk_inode.read().blocks as usize);
|
|
|
|
let fs = self.fs.upgrade().unwrap();
|
|
|
|
|
|
|
|
let to_remove = self.get_disk_block_id(id).unwrap();
|
|
|
|
let to_remove = self.get_disk_block_id(id).unwrap();
|
|
|
|
let current_last = self.get_disk_block_id(self.disk_inode.blocks as usize - 1).unwrap();
|
|
|
|
let current_last = self.get_disk_block_id(self.disk_inode.read().blocks as usize - 1).unwrap();
|
|
|
|
self.set_disk_block_id(id, current_last).unwrap();
|
|
|
|
self.set_disk_block_id(id, current_last).unwrap();
|
|
|
|
self.disk_inode.blocks -= 1;
|
|
|
|
self.disk_inode.write().blocks -= 1;
|
|
|
|
let new_size = self.disk_inode.blocks as usize * BLKSIZE;
|
|
|
|
let new_size = self.disk_inode.read().blocks as usize * BLKSIZE;
|
|
|
|
self._set_size(new_size);
|
|
|
|
self._set_size(new_size);
|
|
|
|
fs.free_block(to_remove);
|
|
|
|
self.fs.free_block(to_remove);
|
|
|
|
Ok(())
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/// Resize content size, no matter what type it is.
|
|
|
|
/// Resize content size, no matter what type it is.
|
|
|
|
fn _resize(&mut self, len: usize) -> vfs::Result<()> {
|
|
|
|
fn _resize(&self, len: usize) -> vfs::Result<()> {
|
|
|
|
assert!(len <= MAX_FILE_SIZE, "file size exceed limit");
|
|
|
|
assert!(len <= MAX_FILE_SIZE, "file size exceed limit");
|
|
|
|
let blocks = ((len + BLKSIZE - 1) / BLKSIZE) as u32;
|
|
|
|
let blocks = ((len + BLKSIZE - 1) / BLKSIZE) as u32;
|
|
|
|
use core::cmp::{Ord, Ordering};
|
|
|
|
use core::cmp::{Ord, Ordering};
|
|
|
|
match blocks.cmp(&self.disk_inode.blocks) {
|
|
|
|
let old_blocks = self.disk_inode.read().blocks;
|
|
|
|
|
|
|
|
match blocks.cmp(&old_blocks) {
|
|
|
|
Ordering::Equal => {} // Do nothing
|
|
|
|
Ordering::Equal => {} // Do nothing
|
|
|
|
Ordering::Greater => {
|
|
|
|
Ordering::Greater => {
|
|
|
|
let fs = self.fs.upgrade().unwrap();
|
|
|
|
{
|
|
|
|
let old_blocks = self.disk_inode.blocks;
|
|
|
|
let mut disk_inode = self.disk_inode.write();
|
|
|
|
self.disk_inode.blocks = blocks;
|
|
|
|
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.alloc_block().unwrap() as u32;
|
|
|
|
disk_inode.indirect = self.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.alloc_block().expect("no more space");
|
|
|
|
let disk_block_id = self.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)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// clean up
|
|
|
|
// clean up
|
|
|
|
let old_size = self._size();
|
|
|
|
let old_size = self._size();
|
|
|
@ -165,18 +160,18 @@ impl INode {
|
|
|
|
self._clean_at(old_size, len).unwrap();
|
|
|
|
self._clean_at(old_size, len).unwrap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ordering::Less => {
|
|
|
|
Ordering::Less => {
|
|
|
|
let fs = self.fs.upgrade().unwrap();
|
|
|
|
|
|
|
|
// free extra blocks
|
|
|
|
// free extra blocks
|
|
|
|
for i in blocks..self.disk_inode.blocks {
|
|
|
|
for i in blocks..old_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.free_block(disk_block_id);
|
|
|
|
self.fs.free_block(disk_block_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut disk_inode = self.disk_inode.write();
|
|
|
|
// 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 && disk_inode.blocks >= NDIRECT as u32 {
|
|
|
|
fs.free_block(self.disk_inode.indirect as usize);
|
|
|
|
self.fs.free_block(disk_inode.indirect as usize);
|
|
|
|
self.disk_inode.indirect = 0;
|
|
|
|
disk_inode.indirect = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.disk_inode.blocks = blocks;
|
|
|
|
disk_inode.blocks = blocks;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self._set_size(len);
|
|
|
|
self._set_size(len);
|
|
|
@ -185,27 +180,27 @@ impl INode {
|
|
|
|
/// Get the actual size of this inode,
|
|
|
|
/// Get the actual size of this inode,
|
|
|
|
/// since size in inode for dir is not real size
|
|
|
|
/// since size in inode for dir is not real size
|
|
|
|
fn _size(&self) -> usize {
|
|
|
|
fn _size(&self) -> usize {
|
|
|
|
match self.disk_inode.type_ {
|
|
|
|
let disk_inode = self.disk_inode.read();
|
|
|
|
FileType::Dir => self.disk_inode.blocks as usize * BLKSIZE,
|
|
|
|
match disk_inode.type_ {
|
|
|
|
FileType::File => self.disk_inode.size as usize,
|
|
|
|
FileType::Dir => disk_inode.blocks as usize * BLKSIZE,
|
|
|
|
|
|
|
|
FileType::File => disk_inode.size as usize,
|
|
|
|
_ => unimplemented!(),
|
|
|
|
_ => unimplemented!(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/// Set the ucore compat size of this inode,
|
|
|
|
/// Set the ucore compat size of this inode,
|
|
|
|
/// Size in inode for dir is size of entries
|
|
|
|
/// Size in inode for dir is size of entries
|
|
|
|
fn _set_size(&mut self, len: usize) {
|
|
|
|
fn _set_size(&self, len: usize) {
|
|
|
|
self.disk_inode.size = match self.disk_inode.type_ {
|
|
|
|
let mut disk_inode = self.disk_inode.write();
|
|
|
|
FileType::Dir => self.disk_inode.blocks as usize * DIRENT_SIZE,
|
|
|
|
disk_inode.size = match disk_inode.type_ {
|
|
|
|
|
|
|
|
FileType::Dir => disk_inode.blocks as usize * DIRENT_SIZE,
|
|
|
|
FileType::File => len,
|
|
|
|
FileType::File => len,
|
|
|
|
_ => unimplemented!(),
|
|
|
|
_ => unimplemented!(),
|
|
|
|
} as u32
|
|
|
|
} as u32
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/// Read/Write content, no matter what type it is
|
|
|
|
/// Read/Write content, no matter what type it is
|
|
|
|
fn _io_at<F>(&self, begin: usize, end: usize, mut f: F) -> vfs::Result<usize>
|
|
|
|
fn _io_at<F>(&self, begin: usize, end: usize, mut f: F) -> vfs::Result<usize>
|
|
|
|
where F: FnMut(RefMut<Box<Device>>, &BlockRange, usize)
|
|
|
|
where F: FnMut(&mut Box<Device>, &BlockRange, usize)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
let fs = self.fs.upgrade().unwrap();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let size = self._size();
|
|
|
|
let size = self._size();
|
|
|
|
let iter = BlockIter {
|
|
|
|
let iter = BlockIter {
|
|
|
|
begin: size.min(begin),
|
|
|
|
begin: size.min(begin),
|
|
|
@ -217,85 +212,86 @@ impl INode {
|
|
|
|
let mut buf_offset = 0usize;
|
|
|
|
let mut buf_offset = 0usize;
|
|
|
|
for mut range in iter {
|
|
|
|
for mut range in iter {
|
|
|
|
range.block = self.get_disk_block_id(range.block).unwrap();
|
|
|
|
range.block = self.get_disk_block_id(range.block).unwrap();
|
|
|
|
f(fs.device.borrow_mut(), &range, buf_offset);
|
|
|
|
f(&mut *self.fs.device.lock(), &range, buf_offset);
|
|
|
|
buf_offset += range.len();
|
|
|
|
buf_offset += range.len();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(buf_offset)
|
|
|
|
Ok(buf_offset)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/// Read content, no matter what type it is
|
|
|
|
/// Read content, no matter what type it is
|
|
|
|
fn _read_at(&self, offset: usize, buf: &mut [u8]) -> vfs::Result<usize> {
|
|
|
|
fn _read_at(&self, offset: usize, buf: &mut [u8]) -> vfs::Result<usize> {
|
|
|
|
self._io_at(offset, offset + buf.len(), |mut device, range, offset| {
|
|
|
|
self._io_at(offset, offset + buf.len(), |device, range, offset| {
|
|
|
|
device.read_block(range.block, range.begin, &mut buf[offset..offset + range.len()]).unwrap()
|
|
|
|
device.read_block(range.block, range.begin, &mut buf[offset..offset + range.len()]).unwrap()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/// Write content, no matter what type it is
|
|
|
|
/// Write content, no matter what type it is
|
|
|
|
fn _write_at(&self, offset: usize, buf: &[u8]) -> vfs::Result<usize> {
|
|
|
|
fn _write_at(&self, offset: usize, buf: &[u8]) -> vfs::Result<usize> {
|
|
|
|
self._io_at(offset, offset + buf.len(), |mut device, range, offset| {
|
|
|
|
self._io_at(offset, offset + buf.len(), |device, range, offset| {
|
|
|
|
device.write_block(range.block, range.begin, &buf[offset..offset + range.len()]).unwrap()
|
|
|
|
device.write_block(range.block, range.begin, &buf[offset..offset + range.len()]).unwrap()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/// Clean content, no matter what type it is
|
|
|
|
/// Clean content, no matter what type it is
|
|
|
|
fn _clean_at(&self, begin: usize, end: usize) -> vfs::Result<()> {
|
|
|
|
fn _clean_at(&self, begin: usize, end: usize) -> vfs::Result<()> {
|
|
|
|
static ZEROS: [u8; BLKSIZE] = [0; BLKSIZE];
|
|
|
|
static ZEROS: [u8; BLKSIZE] = [0; BLKSIZE];
|
|
|
|
self._io_at(begin, end, |mut device, range, _| {
|
|
|
|
self._io_at(begin, end, |device, range, _| {
|
|
|
|
device.write_block(range.block, range.begin, &ZEROS[..range.len()]).unwrap()
|
|
|
|
device.write_block(range.block, range.begin, &ZEROS[..range.len()]).unwrap()
|
|
|
|
}).unwrap();
|
|
|
|
}).unwrap();
|
|
|
|
Ok(())
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fn nlinks_inc(&mut self) {
|
|
|
|
fn nlinks_inc(&self) {
|
|
|
|
self.disk_inode.nlinks += 1;
|
|
|
|
self.disk_inode.write().nlinks += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fn nlinks_dec(&mut self) {
|
|
|
|
fn nlinks_dec(&self) {
|
|
|
|
assert!(self.disk_inode.nlinks > 0);
|
|
|
|
let mut disk_inode = self.disk_inode.write();
|
|
|
|
self.disk_inode.nlinks -= 1;
|
|
|
|
assert!(disk_inode.nlinks > 0);
|
|
|
|
|
|
|
|
disk_inode.nlinks -= 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl vfs::INode for INode {
|
|
|
|
impl vfs::INode for INodeImpl {
|
|
|
|
fn open(&mut self, flags: u32) -> vfs::Result<()> {
|
|
|
|
fn open(&self, flags: u32) -> vfs::Result<()> {
|
|
|
|
// Do nothing
|
|
|
|
// Do nothing
|
|
|
|
Ok(())
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fn close(&mut self) -> vfs::Result<()> {
|
|
|
|
fn close(&self) -> vfs::Result<()> {
|
|
|
|
self.sync()
|
|
|
|
self.sync()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fn read_at(&self, offset: usize, buf: &mut [u8]) -> vfs::Result<usize> {
|
|
|
|
fn read_at(&self, offset: usize, buf: &mut [u8]) -> vfs::Result<usize> {
|
|
|
|
assert_eq!(self.disk_inode.type_, FileType::File, "read_at is only available on file");
|
|
|
|
assert_eq!(self.disk_inode.read().type_, FileType::File, "read_at is only available on file");
|
|
|
|
self._read_at(offset, buf)
|
|
|
|
self._read_at(offset, buf)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fn write_at(&self, offset: usize, buf: &[u8]) -> vfs::Result<usize> {
|
|
|
|
fn write_at(&self, offset: usize, buf: &[u8]) -> vfs::Result<usize> {
|
|
|
|
assert_eq!(self.disk_inode.type_, FileType::File, "write_at is only available on file");
|
|
|
|
assert_eq!(self.disk_inode.read().type_, FileType::File, "write_at is only available on file");
|
|
|
|
self._write_at(offset, buf)
|
|
|
|
self._write_at(offset, buf)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/// the size returned here is logical size(entry num for directory), not the disk space used.
|
|
|
|
/// the size returned here is logical size(entry num for directory), not the disk space used.
|
|
|
|
fn info(&self) -> vfs::Result<vfs::FileInfo> {
|
|
|
|
fn info(&self) -> vfs::Result<vfs::FileInfo> {
|
|
|
|
|
|
|
|
let disk_inode = self.disk_inode.read();
|
|
|
|
Ok(vfs::FileInfo {
|
|
|
|
Ok(vfs::FileInfo {
|
|
|
|
size: match self.disk_inode.type_ {
|
|
|
|
size: match disk_inode.type_ {
|
|
|
|
FileType::File => self.disk_inode.size as usize,
|
|
|
|
FileType::File => disk_inode.size as usize,
|
|
|
|
FileType::Dir => self.disk_inode.blocks as usize,
|
|
|
|
FileType::Dir => disk_inode.blocks as usize,
|
|
|
|
_ => unimplemented!(),
|
|
|
|
_ => unimplemented!(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
mode: 0,
|
|
|
|
mode: 0,
|
|
|
|
type_: vfs::FileType::from(self.disk_inode.type_.clone()),
|
|
|
|
type_: vfs::FileType::from(disk_inode.type_.clone()),
|
|
|
|
blocks: self.disk_inode.blocks as usize,
|
|
|
|
blocks: disk_inode.blocks as usize,
|
|
|
|
nlinks: self.disk_inode.nlinks as usize,
|
|
|
|
nlinks: disk_inode.nlinks as usize,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fn sync(&mut self) -> vfs::Result<()> {
|
|
|
|
fn sync(&self) -> vfs::Result<()> {
|
|
|
|
if self.disk_inode.dirty() {
|
|
|
|
let mut disk_inode = self.disk_inode.write();
|
|
|
|
let fs = self.fs.upgrade().unwrap();
|
|
|
|
if disk_inode.dirty() {
|
|
|
|
fs.device.borrow_mut().write_block(self.id, 0, self.disk_inode.as_buf()).unwrap();
|
|
|
|
self.fs.device.lock().write_block(self.id, 0, disk_inode.as_buf()).unwrap();
|
|
|
|
self.disk_inode.sync();
|
|
|
|
disk_inode.sync();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fn resize(&mut self, len: usize) -> vfs::Result<()> {
|
|
|
|
fn resize(&self, len: usize) -> vfs::Result<()> {
|
|
|
|
assert_eq!(self.disk_inode.type_, FileType::File, "resize is only available on file");
|
|
|
|
assert_eq!(self.disk_inode.read().type_, FileType::File, "resize is only available on file");
|
|
|
|
self._resize(len)
|
|
|
|
self._resize(len)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fn create(&mut self, name: &str, type_: vfs::FileType) -> vfs::Result<Ptr<vfs::INode>> {
|
|
|
|
fn create(&self, name: &str, type_: vfs::FileType) -> vfs::Result<Arc<vfs::INode>> {
|
|
|
|
let fs = self.fs.upgrade().unwrap();
|
|
|
|
let info = self.info()?;
|
|
|
|
let info = self.info().unwrap();
|
|
|
|
|
|
|
|
assert_eq!(info.type_, vfs::FileType::Dir);
|
|
|
|
assert_eq!(info.type_, vfs::FileType::Dir);
|
|
|
|
assert!(info.nlinks > 0);
|
|
|
|
assert!(info.nlinks > 0);
|
|
|
|
|
|
|
|
|
|
|
@ -304,63 +300,57 @@ impl vfs::INode for INode {
|
|
|
|
|
|
|
|
|
|
|
|
// Create new INode
|
|
|
|
// Create new INode
|
|
|
|
let inode = match type_ {
|
|
|
|
let inode = match type_ {
|
|
|
|
vfs::FileType::File => fs.new_inode_file().unwrap(),
|
|
|
|
vfs::FileType::File => self.fs.new_inode_file().unwrap(),
|
|
|
|
vfs::FileType::Dir => fs.new_inode_dir(self.id).unwrap(),
|
|
|
|
vfs::FileType::Dir => self.fs.new_inode_dir(self.id).unwrap(),
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// Write new entry
|
|
|
|
// Write new entry
|
|
|
|
let entry = DiskEntry {
|
|
|
|
let entry = DiskEntry {
|
|
|
|
id: inode.borrow().id as u32,
|
|
|
|
id: inode.id as u32,
|
|
|
|
name: Str256::from(name),
|
|
|
|
name: Str256::from(name),
|
|
|
|
};
|
|
|
|
};
|
|
|
|
let old_size = self._size();
|
|
|
|
let old_size = self._size();
|
|
|
|
self._resize(old_size + BLKSIZE).unwrap();
|
|
|
|
self._resize(old_size + BLKSIZE).unwrap();
|
|
|
|
self._write_at(old_size, entry.as_buf()).unwrap();
|
|
|
|
self._write_at(old_size, entry.as_buf()).unwrap();
|
|
|
|
inode.borrow_mut().nlinks_inc();
|
|
|
|
inode.nlinks_inc();
|
|
|
|
if type_ == vfs::FileType::Dir {
|
|
|
|
if type_ == vfs::FileType::Dir {
|
|
|
|
inode.borrow_mut().nlinks_inc();//for .
|
|
|
|
inode.nlinks_inc(); //for .
|
|
|
|
self.nlinks_inc(); //for ..
|
|
|
|
self.nlinks_inc(); //for ..
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Ok(inode)
|
|
|
|
Ok(inode)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fn unlink(&mut self, name: &str) -> vfs::Result<()> {
|
|
|
|
fn unlink(&self, name: &str) -> vfs::Result<()> {
|
|
|
|
assert!(name != ".");
|
|
|
|
assert!(name != ".");
|
|
|
|
assert!(name != "..");
|
|
|
|
assert!(name != "..");
|
|
|
|
let fs = self.fs.upgrade().unwrap();
|
|
|
|
let info = self.info()?;
|
|
|
|
let info = self.info().unwrap();
|
|
|
|
|
|
|
|
assert_eq!(info.type_, vfs::FileType::Dir);
|
|
|
|
assert_eq!(info.type_, vfs::FileType::Dir);
|
|
|
|
|
|
|
|
|
|
|
|
let inode_and_entry_id = self.get_file_inode_and_entry_id(name);
|
|
|
|
let (inode_id, entry_id) = self.get_file_inode_and_entry_id(name).ok_or(())?;
|
|
|
|
if inode_and_entry_id.is_none() {
|
|
|
|
let inode = self.fs.get_inode(inode_id);
|
|
|
|
return Err(());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
let (inode_id, entry_id) = inode_and_entry_id.unwrap();
|
|
|
|
|
|
|
|
let inode = fs.get_inode(inode_id);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let type_ = inode.borrow().disk_inode.type_;
|
|
|
|
let type_ = inode.disk_inode.read().type_;
|
|
|
|
if type_ == FileType::Dir {
|
|
|
|
if type_ == FileType::Dir {
|
|
|
|
// only . and ..
|
|
|
|
// only . and ..
|
|
|
|
assert_eq!(inode.borrow().disk_inode.blocks, 2);
|
|
|
|
assert_eq!(inode.disk_inode.read().blocks, 2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
inode.borrow_mut().nlinks_dec();
|
|
|
|
inode.nlinks_dec();
|
|
|
|
if type_ == FileType::Dir {
|
|
|
|
if type_ == FileType::Dir {
|
|
|
|
inode.borrow_mut().nlinks_dec();//for .
|
|
|
|
inode.nlinks_dec(); //for .
|
|
|
|
self.nlinks_dec(); //for ..
|
|
|
|
self.nlinks_dec(); //for ..
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.remove_dirent_page(entry_id);
|
|
|
|
self.remove_dirent_page(entry_id);
|
|
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fn link(&mut self, name: &str, other: &mut vfs::INode) -> vfs::Result<()> {
|
|
|
|
fn link(&self, name: &str, other: &Arc<INode>) -> vfs::Result<()> {
|
|
|
|
let fs = self.fs.upgrade().unwrap();
|
|
|
|
let info = self.info()?;
|
|
|
|
let info = self.info().unwrap();
|
|
|
|
|
|
|
|
assert_eq!(info.type_, vfs::FileType::Dir);
|
|
|
|
assert_eq!(info.type_, vfs::FileType::Dir);
|
|
|
|
assert!(info.nlinks > 0);
|
|
|
|
assert!(info.nlinks > 0);
|
|
|
|
assert!(self.get_file_inode_id(name).is_none(), "file name exist");
|
|
|
|
assert!(self.get_file_inode_id(name).is_none(), "file name exist");
|
|
|
|
let child = other.downcast_mut::<INode>().unwrap();
|
|
|
|
let child = other.downcast_ref::<INodeImpl>().unwrap();
|
|
|
|
assert!(Rc::ptr_eq(&fs, &child.fs.upgrade().unwrap()));
|
|
|
|
assert!(Arc::ptr_eq(&self.fs, &child.fs));
|
|
|
|
assert!(child.info().unwrap().type_ != vfs::FileType::Dir);
|
|
|
|
assert!(child.info()?.type_ != vfs::FileType::Dir);
|
|
|
|
let entry = DiskEntry {
|
|
|
|
let entry = DiskEntry {
|
|
|
|
id: child.id as u32,
|
|
|
|
id: child.id as u32,
|
|
|
|
name: Str256::from(name),
|
|
|
|
name: Str256::from(name),
|
|
|
@ -371,18 +361,14 @@ impl vfs::INode for INode {
|
|
|
|
child.nlinks_inc();
|
|
|
|
child.nlinks_inc();
|
|
|
|
Ok(())
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fn rename(&mut self, old_name: &str, new_name: &str) -> vfs::Result<()> {
|
|
|
|
fn rename(&self, old_name: &str, new_name: &str) -> vfs::Result<()> {
|
|
|
|
let info = self.info().unwrap();
|
|
|
|
let info = self.info()?;
|
|
|
|
assert_eq!(info.type_, vfs::FileType::Dir);
|
|
|
|
assert_eq!(info.type_, vfs::FileType::Dir);
|
|
|
|
assert!(info.nlinks > 0);
|
|
|
|
assert!(info.nlinks > 0);
|
|
|
|
|
|
|
|
|
|
|
|
assert!(self.get_file_inode_id(new_name).is_none(), "file name exist");
|
|
|
|
assert!(self.get_file_inode_id(new_name).is_none(), "file name exist");
|
|
|
|
|
|
|
|
|
|
|
|
let inode_and_entry_id = self.get_file_inode_and_entry_id(old_name);
|
|
|
|
let (_, entry_id) = self.get_file_inode_and_entry_id(old_name).ok_or(())?;
|
|
|
|
if inode_and_entry_id.is_none() {
|
|
|
|
|
|
|
|
return Err(());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
let (_, entry_id) = inode_and_entry_id.unwrap();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// in place modify name
|
|
|
|
// in place modify name
|
|
|
|
let mut entry: DiskEntry = unsafe { uninitialized() };
|
|
|
|
let mut entry: DiskEntry = unsafe { uninitialized() };
|
|
|
@ -393,24 +379,19 @@ impl vfs::INode for INode {
|
|
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fn move_(&mut self, old_name: &str, target: &mut vfs::INode, new_name: &str) -> vfs::Result<()> {
|
|
|
|
fn move_(&self, old_name: &str, target: &Arc<INode>, new_name: &str) -> vfs::Result<()> {
|
|
|
|
let fs = self.fs.upgrade().unwrap();
|
|
|
|
let info = self.info()?;
|
|
|
|
let info = self.info().unwrap();
|
|
|
|
|
|
|
|
assert_eq!(info.type_, vfs::FileType::Dir);
|
|
|
|
assert_eq!(info.type_, vfs::FileType::Dir);
|
|
|
|
assert!(info.nlinks > 0);
|
|
|
|
assert!(info.nlinks > 0);
|
|
|
|
let dest = target.downcast_mut::<INode>().unwrap();
|
|
|
|
let dest = target.downcast_ref::<INodeImpl>().unwrap();
|
|
|
|
assert!(Rc::ptr_eq(&fs, &dest.fs.upgrade().unwrap()));
|
|
|
|
assert!(Arc::ptr_eq(&self.fs, &dest.fs));
|
|
|
|
assert!(dest.info().unwrap().type_ == vfs::FileType::Dir);
|
|
|
|
assert!(dest.info()?.type_ == vfs::FileType::Dir);
|
|
|
|
assert!(dest.info().unwrap().nlinks > 0);
|
|
|
|
assert!(dest.info()?.nlinks > 0);
|
|
|
|
|
|
|
|
|
|
|
|
assert!(dest.get_file_inode_id(new_name).is_none(), "file name exist");
|
|
|
|
assert!(dest.get_file_inode_id(new_name).is_none(), "file name exist");
|
|
|
|
|
|
|
|
|
|
|
|
let inode_and_entry_id = self.get_file_inode_and_entry_id(old_name);
|
|
|
|
let (inode_id, entry_id) = self.get_file_inode_and_entry_id(old_name).ok_or(())?;
|
|
|
|
if inode_and_entry_id.is_none() {
|
|
|
|
let inode = self.fs.get_inode(inode_id);
|
|
|
|
return Err(());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
let (inode_id, entry_id) = inode_and_entry_id.unwrap();
|
|
|
|
|
|
|
|
let inode = fs.get_inode(inode_id);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let entry = DiskEntry {
|
|
|
|
let entry = DiskEntry {
|
|
|
|
id: inode_id as u32,
|
|
|
|
id: inode_id as u32,
|
|
|
@ -422,53 +403,42 @@ impl vfs::INode for INode {
|
|
|
|
|
|
|
|
|
|
|
|
self.remove_dirent_page(entry_id);
|
|
|
|
self.remove_dirent_page(entry_id);
|
|
|
|
|
|
|
|
|
|
|
|
if inode.borrow().info().unwrap().type_ == vfs::FileType::Dir {
|
|
|
|
if inode.info()?.type_ == vfs::FileType::Dir {
|
|
|
|
self.nlinks_dec();
|
|
|
|
self.nlinks_dec();
|
|
|
|
dest.nlinks_inc();
|
|
|
|
dest.nlinks_inc();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fn find(&self, name: &str) -> vfs::Result<Ptr<vfs::INode>> {
|
|
|
|
fn find(&self, name: &str) -> vfs::Result<Arc<vfs::INode>> {
|
|
|
|
let fs = self.fs.upgrade().unwrap();
|
|
|
|
let info = self.info()?;
|
|
|
|
let info = self.info().unwrap();
|
|
|
|
|
|
|
|
assert_eq!(info.type_, vfs::FileType::Dir);
|
|
|
|
assert_eq!(info.type_, vfs::FileType::Dir);
|
|
|
|
let inode_id = self.get_file_inode_id(name);
|
|
|
|
let inode_id = self.get_file_inode_id(name).ok_or(())?;
|
|
|
|
if inode_id.is_none() {
|
|
|
|
Ok(self.fs.get_inode(inode_id))
|
|
|
|
return Err(());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
let inode = fs.get_inode(inode_id.unwrap());
|
|
|
|
|
|
|
|
Ok(inode)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fn get_entry(&self, id: usize) -> vfs::Result<String> {
|
|
|
|
fn get_entry(&self, id: usize) -> vfs::Result<String> {
|
|
|
|
assert_eq!(self.disk_inode.type_, FileType::Dir);
|
|
|
|
assert_eq!(self.disk_inode.read().type_, FileType::Dir);
|
|
|
|
assert!(id < self.disk_inode.blocks as usize);
|
|
|
|
assert!(id < self.disk_inode.read().blocks as usize);
|
|
|
|
use vfs::INode;
|
|
|
|
|
|
|
|
let mut entry: DiskEntry = unsafe { uninitialized() };
|
|
|
|
let mut entry: DiskEntry = unsafe { uninitialized() };
|
|
|
|
self._read_at(id as usize * BLKSIZE, entry.as_buf_mut()).unwrap();
|
|
|
|
self._read_at(id as usize * BLKSIZE, entry.as_buf_mut()).unwrap();
|
|
|
|
Ok(String::from(entry.name.as_ref()))
|
|
|
|
Ok(String::from(entry.name.as_ref()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fn fs(&self) -> Weak<vfs::FileSystem> {
|
|
|
|
fn fs(&self) -> Arc<vfs::FileSystem> {
|
|
|
|
self.fs.clone()
|
|
|
|
self.fs.clone()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fn as_any_ref(&self) -> &Any {
|
|
|
|
fn as_any_ref(&self) -> &Any {
|
|
|
|
self
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fn as_any_mut(&mut self) -> &mut Any {
|
|
|
|
|
|
|
|
self
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl Drop for INode {
|
|
|
|
impl Drop for INodeImpl {
|
|
|
|
/// Auto sync when drop
|
|
|
|
/// Auto sync when drop
|
|
|
|
fn drop(&mut self) {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
use vfs::INode;
|
|
|
|
|
|
|
|
self.sync().expect("failed to sync");
|
|
|
|
self.sync().expect("failed to sync");
|
|
|
|
if self.disk_inode.nlinks <= 0 {
|
|
|
|
if self.disk_inode.read().nlinks <= 0 {
|
|
|
|
let fs = self.fs.upgrade().unwrap();
|
|
|
|
|
|
|
|
self._resize(0);
|
|
|
|
self._resize(0);
|
|
|
|
self.disk_inode.sync();
|
|
|
|
self.disk_inode.write().sync();
|
|
|
|
fs.free_block(self.id);
|
|
|
|
self.fs.free_block(self.id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -478,24 +448,24 @@ impl Drop for INode {
|
|
|
|
///
|
|
|
|
///
|
|
|
|
/// ## 内部可变性
|
|
|
|
/// ## 内部可变性
|
|
|
|
/// 为了方便协调外部及INode对SFS的访问,并为日后并行化做准备,
|
|
|
|
/// 为了方便协调外部及INode对SFS的访问,并为日后并行化做准备,
|
|
|
|
/// 将SFS设置为内部可变,即对外接口全部是&self,struct的全部field用RefCell包起来
|
|
|
|
/// 将SFS设置为内部可变,即对外接口全部是&self,struct的全部field用RwLock包起来
|
|
|
|
/// 这样其内部各field均可独立访问
|
|
|
|
/// 这样其内部各field均可独立访问
|
|
|
|
pub struct SimpleFileSystem {
|
|
|
|
pub struct SimpleFileSystem {
|
|
|
|
/// on-disk superblock
|
|
|
|
/// on-disk superblock
|
|
|
|
super_block: RefCell<Dirty<SuperBlock>>,
|
|
|
|
super_block: RwLock<Dirty<SuperBlock>>,
|
|
|
|
/// blocks in use are mared 0
|
|
|
|
/// blocks in use are mared 0
|
|
|
|
free_map: RefCell<Dirty<BitVec>>,
|
|
|
|
free_map: RwLock<Dirty<BitVec>>,
|
|
|
|
/// inode list
|
|
|
|
/// inode list
|
|
|
|
inodes: RefCell<BTreeMap<INodeId, Ptr<INode>>>,
|
|
|
|
inodes: RwLock<BTreeMap<INodeId, Weak<INodeImpl>>>,
|
|
|
|
/// device
|
|
|
|
/// device
|
|
|
|
device: RefCell<Box<Device>>,
|
|
|
|
device: Mutex<Box<Device>>,
|
|
|
|
/// Pointer to self, used by INodes
|
|
|
|
/// Pointer to self, used by INodes
|
|
|
|
self_ptr: Weak<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<Rc<Self>> {
|
|
|
|
pub fn open(mut device: Box<Device>) -> Option<Arc<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;
|
|
|
@ -503,15 +473,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: RefCell::new(Dirty::new(super_block)),
|
|
|
|
super_block: RwLock::new(Dirty::new(super_block)),
|
|
|
|
free_map: RefCell::new(Dirty::new(BitVec::from_bytes(&free_map))),
|
|
|
|
free_map: RwLock::new(Dirty::new(BitVec::from_bytes(&free_map))),
|
|
|
|
inodes: RefCell::new(BTreeMap::<INodeId, Ptr<INode>>::new()),
|
|
|
|
inodes: RwLock::new(BTreeMap::new()),
|
|
|
|
device: RefCell::new(device),
|
|
|
|
device: Mutex::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) -> Rc<Self> {
|
|
|
|
pub fn create(mut device: Box<Device>, space: usize) -> Arc<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");
|
|
|
|
|
|
|
|
|
|
|
@ -530,102 +500,105 @@ impl SimpleFileSystem {
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
let sfs = SimpleFileSystem {
|
|
|
|
let sfs = SimpleFileSystem {
|
|
|
|
super_block: RefCell::new(Dirty::new_dirty(super_block)),
|
|
|
|
super_block: RwLock::new(Dirty::new_dirty(super_block)),
|
|
|
|
free_map: RefCell::new(Dirty::new_dirty(free_map)),
|
|
|
|
free_map: RwLock::new(Dirty::new_dirty(free_map)),
|
|
|
|
inodes: RefCell::new(BTreeMap::new()),
|
|
|
|
inodes: RwLock::new(BTreeMap::new()),
|
|
|
|
device: RefCell::new(device),
|
|
|
|
device: Mutex::new(device),
|
|
|
|
self_ptr: Weak::default(),
|
|
|
|
self_ptr: Weak::default(),
|
|
|
|
}.wrap();
|
|
|
|
}.wrap();
|
|
|
|
|
|
|
|
|
|
|
|
// Init root INode
|
|
|
|
// Init root INode
|
|
|
|
use vfs::INode;
|
|
|
|
|
|
|
|
let root = sfs._new_inode(BLKN_ROOT, Dirty::new_dirty(DiskINode::new_dir()));
|
|
|
|
let root = sfs._new_inode(BLKN_ROOT, Dirty::new_dirty(DiskINode::new_dir()));
|
|
|
|
root.borrow_mut().init_dir_entry(BLKN_ROOT).unwrap();
|
|
|
|
root.init_dir_entry(BLKN_ROOT).unwrap();
|
|
|
|
root.borrow_mut().nlinks_inc();//for .
|
|
|
|
root.nlinks_inc(); //for .
|
|
|
|
root.borrow_mut().nlinks_inc();//for ..(root's parent is itself)
|
|
|
|
root.nlinks_inc(); //for ..(root's parent is itself)
|
|
|
|
root.borrow_mut().sync().unwrap();
|
|
|
|
root.sync().unwrap();
|
|
|
|
|
|
|
|
|
|
|
|
sfs
|
|
|
|
sfs
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/// Wrap pure SimpleFileSystem with Rc
|
|
|
|
/// Wrap pure SimpleFileSystem with Arc
|
|
|
|
/// Used in constructors
|
|
|
|
/// Used in constructors
|
|
|
|
fn wrap(self) -> Rc<Self> {
|
|
|
|
fn wrap(self) -> Arc<Self> {
|
|
|
|
// Create a Rc, make a Weak from it, then put it into the struct.
|
|
|
|
// Create a Arc, make a Weak from it, then put it into the struct.
|
|
|
|
// It's a little tricky.
|
|
|
|
// It's a little tricky.
|
|
|
|
let fs = Rc::new(self);
|
|
|
|
let fs = Arc::new(self);
|
|
|
|
let weak = Rc::downgrade(&fs);
|
|
|
|
let weak = Arc::downgrade(&fs);
|
|
|
|
let ptr = Rc::into_raw(fs) as *mut Self;
|
|
|
|
let ptr = Arc::into_raw(fs) as *mut Self;
|
|
|
|
unsafe { (*ptr).self_ptr = weak; }
|
|
|
|
unsafe { (*ptr).self_ptr = weak; }
|
|
|
|
unsafe { Rc::from_raw(ptr) }
|
|
|
|
unsafe { Arc::from_raw(ptr) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// 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 id = self.free_map.borrow_mut().alloc();
|
|
|
|
let id = self.free_map.write().alloc();
|
|
|
|
if id.is_some() {
|
|
|
|
if id.is_some() {
|
|
|
|
self.super_block.borrow_mut().unused_blocks -= 1; // will panic if underflow
|
|
|
|
self.super_block.write().unused_blocks -= 1; // will panic if underflow
|
|
|
|
id
|
|
|
|
id
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
self.flush_unreachable_inodes();
|
|
|
|
self.flush_unreachable_inodes();
|
|
|
|
let id = self.free_map.borrow_mut().alloc();
|
|
|
|
let id = self.free_map.write().alloc();
|
|
|
|
if id.is_some() {
|
|
|
|
if id.is_some() {
|
|
|
|
self.super_block.borrow_mut().unused_blocks -= 1;
|
|
|
|
self.super_block.write().unused_blocks -= 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
id
|
|
|
|
id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/// Free a block
|
|
|
|
/// Free a block
|
|
|
|
fn free_block(&self, block_id: usize) {
|
|
|
|
fn free_block(&self, block_id: usize) {
|
|
|
|
let mut free_map = self.free_map.borrow_mut();
|
|
|
|
let mut free_map = self.free_map.write();
|
|
|
|
assert!(!free_map[block_id]);
|
|
|
|
assert!(!free_map[block_id]);
|
|
|
|
free_map.set(block_id, true);
|
|
|
|
free_map.set(block_id, true);
|
|
|
|
self.super_block.borrow_mut().unused_blocks += 1;
|
|
|
|
self.super_block.write().unused_blocks += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Create a new INode struct, then insert it to self.inodes
|
|
|
|
/// Create a new INode struct, then insert it to self.inodes
|
|
|
|
/// Private used for load or create INode
|
|
|
|
/// Private used for load or create INode
|
|
|
|
fn _new_inode(&self, id: INodeId, disk_inode: Dirty<DiskINode>) -> Ptr<INode> {
|
|
|
|
fn _new_inode(&self, id: INodeId, disk_inode: Dirty<DiskINode>) -> Arc<INodeImpl> {
|
|
|
|
let inode = Rc::new(RefCell::new(INode {
|
|
|
|
let inode = Arc::new(INodeImpl {
|
|
|
|
disk_inode,
|
|
|
|
|
|
|
|
id,
|
|
|
|
id,
|
|
|
|
fs: self.self_ptr.clone(),
|
|
|
|
disk_inode: RwLock::new(disk_inode),
|
|
|
|
}));
|
|
|
|
fs: self.self_ptr.upgrade().unwrap(),
|
|
|
|
self.inodes.borrow_mut().insert(id, inode.clone());
|
|
|
|
});
|
|
|
|
|
|
|
|
self.inodes.write().insert(id, Arc::downgrade(&inode));
|
|
|
|
inode
|
|
|
|
inode
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/// 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(&self, id: INodeId) -> Ptr<INode> {
|
|
|
|
fn get_inode(&self, id: INodeId) -> Arc<INodeImpl> {
|
|
|
|
assert!(!self.free_map.borrow()[id]);
|
|
|
|
assert!(!self.free_map.read()[id]);
|
|
|
|
|
|
|
|
|
|
|
|
// Load if not in memory.
|
|
|
|
// In the BTreeSet and not weak.
|
|
|
|
if !self.inodes.borrow().contains_key(&id) {
|
|
|
|
if let Some(inode) = self.inodes.read().get(&id) {
|
|
|
|
let disk_inode = Dirty::new(self.device.borrow_mut().load_struct::<DiskINode>(id));
|
|
|
|
if let Some(inode) = inode.upgrade() {
|
|
|
|
self._new_inode(id, disk_inode)
|
|
|
|
return inode;
|
|
|
|
} else {
|
|
|
|
|
|
|
|
self.inodes.borrow_mut().get(&id).unwrap().clone()
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load if not in set, or is weak ref.
|
|
|
|
|
|
|
|
let disk_inode = Dirty::new(self.device.lock().load_struct::<DiskINode>(id));
|
|
|
|
|
|
|
|
self._new_inode(id, disk_inode)
|
|
|
|
|
|
|
|
}
|
|
|
|
/// Create a new INode file
|
|
|
|
/// Create a new INode file
|
|
|
|
fn new_inode_file(&self) -> vfs::Result<Ptr<INode>> {
|
|
|
|
fn new_inode_file(&self) -> vfs::Result<Arc<INodeImpl>> {
|
|
|
|
let id = self.alloc_block().unwrap();
|
|
|
|
let id = self.alloc_block().unwrap();
|
|
|
|
let disk_inode = Dirty::new_dirty(DiskINode::new_file());
|
|
|
|
let disk_inode = Dirty::new_dirty(DiskINode::new_file());
|
|
|
|
Ok(self._new_inode(id, disk_inode))
|
|
|
|
Ok(self._new_inode(id, disk_inode))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/// Create a new INode dir
|
|
|
|
/// Create a new INode dir
|
|
|
|
fn new_inode_dir(&self, parent: INodeId) -> vfs::Result<Ptr<INode>> {
|
|
|
|
fn new_inode_dir(&self, parent: INodeId) -> vfs::Result<Arc<INodeImpl>> {
|
|
|
|
let id = self.alloc_block().unwrap();
|
|
|
|
let id = self.alloc_block().unwrap();
|
|
|
|
let disk_inode = Dirty::new_dirty(DiskINode::new_dir());
|
|
|
|
let disk_inode = Dirty::new_dirty(DiskINode::new_dir());
|
|
|
|
let inode = self._new_inode(id, disk_inode);
|
|
|
|
let inode = self._new_inode(id, disk_inode);
|
|
|
|
inode.borrow_mut().init_dir_entry(parent).unwrap();
|
|
|
|
inode.init_dir_entry(parent).unwrap();
|
|
|
|
Ok(inode)
|
|
|
|
Ok(inode)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fn flush_unreachable_inodes(&self) {
|
|
|
|
fn flush_unreachable_inodes(&self) {
|
|
|
|
let mut inodes = self.inodes.borrow_mut();
|
|
|
|
let mut inodes = self.inodes.write();
|
|
|
|
let remove_ids: Vec<_> = inodes.iter().filter(|(_, inode)| {
|
|
|
|
let remove_ids: Vec<_> = inodes.iter().filter(|(_, inode)| {
|
|
|
|
use vfs::INode;
|
|
|
|
if let Some(inode) = inode.upgrade() {
|
|
|
|
Rc::strong_count(inode) <= 1
|
|
|
|
Arc::strong_count(&inode) <= 1 && inode.info().unwrap().nlinks == 0
|
|
|
|
&& inode.borrow().info().unwrap().nlinks == 0
|
|
|
|
} else {
|
|
|
|
|
|
|
|
true
|
|
|
|
|
|
|
|
}
|
|
|
|
}).map(|(&id, _)| id).collect();
|
|
|
|
}).map(|(&id, _)| id).collect();
|
|
|
|
for id in remove_ids.iter() {
|
|
|
|
for id in remove_ids.iter() {
|
|
|
|
inodes.remove(&id);
|
|
|
|
inodes.remove(&id);
|
|
|
@ -636,25 +609,26 @@ impl SimpleFileSystem {
|
|
|
|
impl vfs::FileSystem for SimpleFileSystem {
|
|
|
|
impl vfs::FileSystem for SimpleFileSystem {
|
|
|
|
/// Write back super block if dirty
|
|
|
|
/// Write back super block if dirty
|
|
|
|
fn sync(&self) -> vfs::Result<()> {
|
|
|
|
fn sync(&self) -> vfs::Result<()> {
|
|
|
|
let mut super_block = self.super_block.borrow_mut();
|
|
|
|
let mut super_block = self.super_block.write();
|
|
|
|
if super_block.dirty() {
|
|
|
|
if super_block.dirty() {
|
|
|
|
self.device.borrow_mut().write_at(BLKSIZE * BLKN_SUPER, super_block.as_buf()).unwrap();
|
|
|
|
self.device.lock().write_at(BLKSIZE * BLKN_SUPER, super_block.as_buf()).unwrap();
|
|
|
|
super_block.sync();
|
|
|
|
super_block.sync();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let mut free_map = self.free_map.borrow_mut();
|
|
|
|
let mut free_map = self.free_map.write();
|
|
|
|
if free_map.dirty() {
|
|
|
|
if free_map.dirty() {
|
|
|
|
self.device.borrow_mut().write_at(BLKSIZE * BLKN_FREEMAP, free_map.as_buf()).unwrap();
|
|
|
|
self.device.lock().write_at(BLKSIZE * BLKN_FREEMAP, free_map.as_buf()).unwrap();
|
|
|
|
free_map.sync();
|
|
|
|
free_map.sync();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for inode in self.inodes.borrow().values() {
|
|
|
|
for inode in self.inodes.read().values() {
|
|
|
|
use vfs::INode;
|
|
|
|
if let Some(inode) = inode.upgrade() {
|
|
|
|
inode.borrow_mut().sync().unwrap();
|
|
|
|
inode.sync().unwrap();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.flush_unreachable_inodes();
|
|
|
|
self.flush_unreachable_inodes();
|
|
|
|
Ok(())
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn root_inode(&self) -> Ptr<vfs::INode> {
|
|
|
|
fn root_inode(&self) -> Arc<vfs::INode> {
|
|
|
|
self.get_inode(BLKN_ROOT)
|
|
|
|
self.get_inode(BLKN_ROOT)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|