Merge branch 'dev' into main

This commit is contained in:
Yifan Wu 2021-03-06 03:33:47 +08:00
commit e214d0f91b
5 changed files with 27 additions and 73 deletions

View file

@ -119,12 +119,6 @@ impl EasyFileSystem {
) )
} }
/*
fn get_super_block(&self) -> Dirty<SuperBlock> {
Dirty::new(0, 0, self.block_device.clone())
}
*/
pub fn get_disk_inode_pos(&self, inode_id: u32) -> (u32, usize) { pub fn get_disk_inode_pos(&self, inode_id: u32) -> (u32, usize) {
let inode_size = core::mem::size_of::<DiskInode>(); let inode_size = core::mem::size_of::<DiskInode>();
let inodes_per_block = (BLOCK_SZ / inode_size) as u32; let inodes_per_block = (BLOCK_SZ / inode_size) as u32;
@ -136,16 +130,6 @@ impl EasyFileSystem {
self.data_area_start_block + data_block_id self.data_area_start_block + data_block_id
} }
/*
fn get_block(&self, block_id: u32) -> Dirty<DataBlock> {
Dirty::new(
block_id as usize,
0,
self.block_device.clone(),
)
}
*/
pub fn alloc_inode(&mut self) -> u32 { pub fn alloc_inode(&mut self) -> u32 {
self.inode_bitmap.alloc(&self.block_device).unwrap() as u32 self.inode_bitmap.alloc(&self.block_device).unwrap() as u32
} }

View file

@ -72,7 +72,6 @@ type IndirectBlock = [u32; BLOCK_SZ / 4];
type DataBlock = [u8; BLOCK_SZ]; type DataBlock = [u8; BLOCK_SZ];
#[repr(C)] #[repr(C)]
/// Only support level-1 indirect now, **indirect2** field is always 0.
pub struct DiskInode { pub struct DiskInode {
pub size: u32, pub size: u32,
pub direct: [u32; INODE_DIRECT_COUNT], pub direct: [u32; INODE_DIRECT_COUNT],
@ -235,32 +234,6 @@ impl DiskInode {
} }
}); });
} }
/*
pub fn clear_size(&mut self, block_device: &Arc<dyn BlockDevice>) -> Vec<u32> {
let mut v: Vec<u32> = Vec::new();
let blocks = self.blocks() as usize;
self.size = 0;
for i in 0..blocks.min(INODE_DIRECT_COUNT) {
v.push(self.direct[i]);
self.direct[i] = 0;
}
if blocks > INODE_DIRECT_COUNT {
get_block_cache(
self.indirect1 as usize,
Arc::clone(block_device),
)
.lock()
.modify(0, |indirect_block: &mut IndirectBlock| {
for i in 0..blocks - INODE_DIRECT_COUNT {
v.push(indirect_block[i]);
indirect_block[i] = 0;
}
});
}
v
}
*/
/// Clear size to zero and return blocks that should be deallocated. /// Clear size to zero and return blocks that should be deallocated.
/// ///
@ -434,10 +407,13 @@ pub struct DirEntry {
pub const DIRENT_SZ: usize = 32; pub const DIRENT_SZ: usize = 32;
//pub type DirentBlock = [DirEntry; BLOCK_SZ / DIRENT_SZ];
pub type DirentBytes = [u8; DIRENT_SZ];
impl DirEntry { impl DirEntry {
pub fn empty() -> Self {
Self {
name: [0u8; NAME_LENGTH_LIMIT + 1],
inode_number: 0,
}
}
pub fn new(name: &str, inode_number: u32) -> Self { pub fn new(name: &str, inode_number: u32) -> Self {
let mut bytes = [0u8; NAME_LENGTH_LIMIT + 1]; let mut bytes = [0u8; NAME_LENGTH_LIMIT + 1];
&mut bytes[..name.len()].copy_from_slice(name.as_bytes()); &mut bytes[..name.len()].copy_from_slice(name.as_bytes());
@ -446,18 +422,20 @@ impl DirEntry {
inode_number, inode_number,
} }
} }
pub fn into_bytes(&self) -> &DirentBytes { pub fn as_bytes(&self) -> &[u8] {
unsafe { unsafe {
&*(self as *const Self as usize as *const DirentBytes) core::slice::from_raw_parts(
self as *const _ as usize as *const u8,
DIRENT_SZ,
)
} }
} }
pub fn from_bytes(bytes: &DirentBytes) -> &Self { pub fn as_bytes_mut(&mut self) -> &mut [u8] {
unsafe { &*(bytes.as_ptr() as usize as *const Self) }
}
#[allow(unused)]
pub fn from_bytes_mut(bytes: &mut DirentBytes) -> &mut Self {
unsafe { unsafe {
&mut *(bytes.as_mut_ptr() as usize as *mut Self) core::slice::from_raw_parts_mut(
self as *mut _ as usize as *mut u8,
DIRENT_SZ,
)
} }
} }
pub fn name(&self) -> &str { pub fn name(&self) -> &str {

View file

@ -3,7 +3,6 @@ use super::{
DiskInode, DiskInode,
DiskInodeType, DiskInodeType,
DirEntry, DirEntry,
DirentBytes,
EasyFileSystem, EasyFileSystem,
DIRENT_SZ, DIRENT_SZ,
get_block_cache, get_block_cache,
@ -49,12 +48,6 @@ impl Inode {
).lock().modify(self.block_offset, f) ).lock().modify(self.block_offset, f)
} }
/*
fn get_disk_inode(&self, fs: &mut MutexGuard<EasyFileSystem>) -> Dirty<DiskInode> {
fs.get_disk_inode(self.inode_id)
}
*/
fn find_inode_id( fn find_inode_id(
&self, &self,
name: &str, name: &str,
@ -63,17 +56,16 @@ impl Inode {
// assert it is a directory // assert it is a directory
assert!(disk_inode.is_dir()); assert!(disk_inode.is_dir());
let file_count = (disk_inode.size as usize) / DIRENT_SZ; let file_count = (disk_inode.size as usize) / DIRENT_SZ;
let mut dirent_space: DirentBytes = Default::default(); let mut dirent = DirEntry::empty();
for i in 0..file_count { for i in 0..file_count {
assert_eq!( assert_eq!(
disk_inode.read_at( disk_inode.read_at(
DIRENT_SZ * i, DIRENT_SZ * i,
&mut dirent_space, dirent.as_bytes_mut(),
&self.block_device, &self.block_device,
), ),
DIRENT_SZ, DIRENT_SZ,
); );
let dirent = DirEntry::from_bytes(&dirent_space);
if dirent.name() == name { if dirent.name() == name {
return Some(dirent.inode_number() as u32); return Some(dirent.inode_number() as u32);
} }
@ -144,7 +136,7 @@ impl Inode {
let dirent = DirEntry::new(name, new_inode_id); let dirent = DirEntry::new(name, new_inode_id);
root_inode.write_at( root_inode.write_at(
file_count * DIRENT_SZ, file_count * DIRENT_SZ,
dirent.into_bytes(), dirent.as_bytes(),
&self.block_device, &self.block_device,
); );
}); });
@ -164,16 +156,16 @@ impl Inode {
let file_count = (disk_inode.size as usize) / DIRENT_SZ; let file_count = (disk_inode.size as usize) / DIRENT_SZ;
let mut v: Vec<String> = Vec::new(); let mut v: Vec<String> = Vec::new();
for i in 0..file_count { for i in 0..file_count {
let mut dirent_bytes: DirentBytes = Default::default(); let mut dirent = DirEntry::empty();
assert_eq!( assert_eq!(
disk_inode.read_at( disk_inode.read_at(
i * DIRENT_SZ, i * DIRENT_SZ,
&mut dirent_bytes, dirent.as_bytes_mut(),
&self.block_device, &self.block_device,
), ),
DIRENT_SZ, DIRENT_SZ,
); );
v.push(String::from(DirEntry::from_bytes(&dirent_bytes).name())); v.push(String::from(dirent.name()));
} }
v v
}) })

View file

@ -71,10 +71,9 @@ disasm-vim: kernel
@vim $(DISASM_TMP) @vim $(DISASM_TMP)
@rm $(DISASM_TMP) @rm $(DISASM_TMP)
run: tools run-inner run: run-inner
tools:
(which $(K210-BURNER)) || (cd .. && git clone https://github.com/sipeed/kflash.py.git && mv kflash.py tools)
run-inner: build run-inner: build
ifeq ($(BOARD),qemu) ifeq ($(BOARD),qemu)
@ -86,6 +85,7 @@ ifeq ($(BOARD),qemu)
-drive file=$(FS_IMG),if=none,format=raw,id=x0 \ -drive file=$(FS_IMG),if=none,format=raw,id=x0 \
-device virtio-blk-device,drive=x0,bus=virtio-mmio-bus.0 -device virtio-blk-device,drive=x0,bus=virtio-mmio-bus.0
else else
(which $(K210-BURNER)) || (cd .. && git clone https://github.com/sipeed/kflash.py.git && mv kflash.py tools)
@cp $(BOOTLOADER) $(BOOTLOADER).copy @cp $(BOOTLOADER) $(BOOTLOADER).copy
@dd if=$(KERNEL_BIN) of=$(BOOTLOADER).copy bs=$(K210_BOOTLOADER_SIZE) seek=1 @dd if=$(KERNEL_BIN) of=$(BOOTLOADER).copy bs=$(K210_BOOTLOADER_SIZE) seek=1
@mv $(BOOTLOADER).copy $(KERNEL_BIN) @mv $(BOOTLOADER).copy $(KERNEL_BIN)
@ -100,4 +100,4 @@ debug: build
tmux split-window -h "riscv64-unknown-elf-gdb -ex 'file $(KERNEL_ELF)' -ex 'set arch riscv:rv64' -ex 'target remote localhost:1234'" && \ tmux split-window -h "riscv64-unknown-elf-gdb -ex 'file $(KERNEL_ELF)' -ex 'set arch riscv:rv64' -ex 'target remote localhost:1234'" && \
tmux -2 attach-session -d tmux -2 attach-session -d
.PHONY: build env kernel clean disasm disasm-vim run-inner tools .PHONY: build env kernel clean disasm disasm-vim run-inner

View file

@ -18,7 +18,7 @@ pub const CLOCK_FREQ: usize = 12500000;
#[cfg(feature = "board_qemu")] #[cfg(feature = "board_qemu")]
pub const MMIO: &[(usize, usize)] = &[ pub const MMIO: &[(usize, usize)] = &[
(0x10000000, 0x10000), (0x10001000, 0x1000),
]; ];
#[cfg(feature = "board_k210")] #[cfg(feature = "board_k210")]