Import easy-fs in os && change easy-fs to no_std mode.
This commit is contained in:
parent
94267a47c4
commit
760de97155
6 changed files with 7 additions and 17 deletions
|
@ -9,7 +9,6 @@ use std::fs::{File, OpenOptions, read_dir};
|
||||||
use std::io::{Read, Write, Seek, SeekFrom};
|
use std::io::{Read, Write, Seek, SeekFrom};
|
||||||
use std::sync::Mutex;
|
use std::sync::Mutex;
|
||||||
use alloc::sync::Arc;
|
use alloc::sync::Arc;
|
||||||
use rand;
|
|
||||||
|
|
||||||
const BLOCK_SZ: usize = 512;
|
const BLOCK_SZ: usize = 512;
|
||||||
|
|
||||||
|
@ -17,7 +16,6 @@ struct BlockFile(Mutex<File>);
|
||||||
|
|
||||||
impl BlockDevice for BlockFile {
|
impl BlockDevice for BlockFile {
|
||||||
fn read_block(&self, block_id: usize, buf: &mut [u8]) {
|
fn read_block(&self, block_id: usize, buf: &mut [u8]) {
|
||||||
println!("reading block {}", block_id);
|
|
||||||
let mut file = self.0.lock().unwrap();
|
let mut file = self.0.lock().unwrap();
|
||||||
file.seek(SeekFrom::Start((block_id * BLOCK_SZ) as u64))
|
file.seek(SeekFrom::Start((block_id * BLOCK_SZ) as u64))
|
||||||
.expect("Error when seeking!");
|
.expect("Error when seeking!");
|
||||||
|
@ -25,7 +23,6 @@ impl BlockDevice for BlockFile {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn write_block(&self, block_id: usize, buf: &[u8]) {
|
fn write_block(&self, block_id: usize, buf: &[u8]) {
|
||||||
println!("writing block {}", block_id);
|
|
||||||
let mut file = self.0.lock().unwrap();
|
let mut file = self.0.lock().unwrap();
|
||||||
file.seek(SeekFrom::Start((block_id * BLOCK_SZ) as u64))
|
file.seek(SeekFrom::Start((block_id * BLOCK_SZ) as u64))
|
||||||
.expect("Error when seeking!");
|
.expect("Error when seeking!");
|
||||||
|
@ -46,7 +43,7 @@ fn easy_fs_pack() -> std::io::Result<()> {
|
||||||
.write(true)
|
.write(true)
|
||||||
.create(true)
|
.create(true)
|
||||||
.open(format!("{}{}", TARGET_PATH, "fs.img"))?;
|
.open(format!("{}{}", TARGET_PATH, "fs.img"))?;
|
||||||
f.set_len(8192 * 512);
|
f.set_len(8192 * 512).unwrap();
|
||||||
f
|
f
|
||||||
})));
|
})));
|
||||||
// 4MiB, at most 4095 files
|
// 4MiB, at most 4095 files
|
||||||
|
@ -55,7 +52,7 @@ fn easy_fs_pack() -> std::io::Result<()> {
|
||||||
8192,
|
8192,
|
||||||
1,
|
1,
|
||||||
);
|
);
|
||||||
let mut root_inode = Arc::new(EasyFileSystem::root_inode(&efs));
|
let root_inode = Arc::new(EasyFileSystem::root_inode(&efs));
|
||||||
let apps: Vec<_> = read_dir("../user/src/bin")
|
let apps: Vec<_> = read_dir("../user/src/bin")
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.into_iter()
|
.into_iter()
|
||||||
|
@ -69,7 +66,7 @@ fn easy_fs_pack() -> std::io::Result<()> {
|
||||||
// load app data from host file system
|
// load app data from host file system
|
||||||
let mut host_file = File::open(format!("{}{}", TARGET_PATH, app)).unwrap();
|
let mut host_file = File::open(format!("{}{}", TARGET_PATH, app)).unwrap();
|
||||||
let mut all_data: Vec<u8> = Vec::new();
|
let mut all_data: Vec<u8> = Vec::new();
|
||||||
let app_size = host_file.read_to_end(&mut all_data).unwrap();
|
host_file.read_to_end(&mut all_data).unwrap();
|
||||||
// create a file in easy-fs
|
// create a file in easy-fs
|
||||||
let inode = root_inode.create(app.as_str()).unwrap();
|
let inode = root_inode.create(app.as_str()).unwrap();
|
||||||
// write data to easy-fs
|
// write data to easy-fs
|
||||||
|
@ -119,6 +116,7 @@ fn efs_test() -> std::io::Result<()> {
|
||||||
0,
|
0,
|
||||||
);
|
);
|
||||||
let mut str = String::new();
|
let mut str = String::new();
|
||||||
|
use rand;
|
||||||
// random digit
|
// random digit
|
||||||
for _ in 0..len {
|
for _ in 0..len {
|
||||||
str.push(char::from('0' as u8 + rand::random::<u8>() % 10));
|
str.push(char::from('0' as u8 + rand::random::<u8>() % 10));
|
||||||
|
|
|
@ -82,7 +82,6 @@ impl EasyFileSystem {
|
||||||
let super_block_dirty: Dirty<SuperBlock> = Dirty::new(0, 0, block_device.clone());
|
let super_block_dirty: Dirty<SuperBlock> = Dirty::new(0, 0, block_device.clone());
|
||||||
let super_block = super_block_dirty.get_ref();
|
let super_block = super_block_dirty.get_ref();
|
||||||
assert!(super_block.is_valid(), "Error loading EFS!");
|
assert!(super_block.is_valid(), "Error loading EFS!");
|
||||||
println!("{:?}", super_block);
|
|
||||||
let inode_total_blocks =
|
let inode_total_blocks =
|
||||||
super_block.inode_bitmap_blocks + super_block.inode_area_blocks;
|
super_block.inode_bitmap_blocks + super_block.inode_area_blocks;
|
||||||
let efs = Self {
|
let efs = Self {
|
||||||
|
|
|
@ -122,7 +122,6 @@ impl DiskInode {
|
||||||
new_blocks: Vec<u32>,
|
new_blocks: Vec<u32>,
|
||||||
block_device: &Arc<dyn BlockDevice>,
|
block_device: &Arc<dyn BlockDevice>,
|
||||||
) {
|
) {
|
||||||
println!("increase_size new_size={}, new_blocks={:?}", new_size, new_blocks);
|
|
||||||
assert_eq!(new_blocks.len() as u32, self.blocks_num_needed(new_size));
|
assert_eq!(new_blocks.len() as u32, self.blocks_num_needed(new_size));
|
||||||
let last_blocks = self.blocks();
|
let last_blocks = self.blocks();
|
||||||
self.size = new_size;
|
self.size = new_size;
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
#![no_std]
|
||||||
|
|
||||||
extern crate alloc;
|
extern crate alloc;
|
||||||
|
|
||||||
mod block_dev;
|
mod block_dev;
|
||||||
|
|
|
@ -41,13 +41,11 @@ impl Inode {
|
||||||
name: &str,
|
name: &str,
|
||||||
inode: &Dirty<DiskInode>,
|
inode: &Dirty<DiskInode>,
|
||||||
) -> Option<u32> {
|
) -> Option<u32> {
|
||||||
println!("into find_inode_id");
|
|
||||||
// assert it is a directory
|
// assert it is a directory
|
||||||
assert!(inode.read(|inode| inode.is_dir()));
|
assert!(inode.read(|inode| inode.is_dir()));
|
||||||
let file_count = inode.read(|inode| {
|
let file_count = inode.read(|inode| {
|
||||||
inode.size as usize
|
inode.size as usize
|
||||||
}) / DIRENT_SZ;
|
}) / DIRENT_SZ;
|
||||||
println!("file_count = {}", file_count);
|
|
||||||
let mut dirent_space: DirentBytes = Default::default();
|
let mut dirent_space: DirentBytes = Default::default();
|
||||||
for i in 0..file_count {
|
for i in 0..file_count {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
|
@ -94,7 +92,6 @@ impl Inode {
|
||||||
let blocks_needed = inode.read(|inode| {
|
let blocks_needed = inode.read(|inode| {
|
||||||
inode.blocks_num_needed(new_size)
|
inode.blocks_num_needed(new_size)
|
||||||
});
|
});
|
||||||
println!("blocks_num_needed = {}", blocks_needed);
|
|
||||||
let mut v: Vec<u32> = Vec::new();
|
let mut v: Vec<u32> = Vec::new();
|
||||||
for _ in 0..blocks_needed {
|
for _ in 0..blocks_needed {
|
||||||
v.push(fs.alloc_data());
|
v.push(fs.alloc_data());
|
||||||
|
@ -106,7 +103,6 @@ impl Inode {
|
||||||
|
|
||||||
pub fn create(&self, name: &str) -> Option<Arc<Inode>> {
|
pub fn create(&self, name: &str) -> Option<Arc<Inode>> {
|
||||||
let mut fs = self.fs.lock();
|
let mut fs = self.fs.lock();
|
||||||
println!("creating name {}", name);
|
|
||||||
let mut inode = self.get_disk_inode(&mut fs);
|
let mut inode = self.get_disk_inode(&mut fs);
|
||||||
// assert it is a directory
|
// assert it is a directory
|
||||||
assert!(inode.read(|inode| inode.is_dir()));
|
assert!(inode.read(|inode| inode.is_dir()));
|
||||||
|
@ -114,13 +110,11 @@ impl Inode {
|
||||||
if let Some(_) = self.find_inode_id(name, &inode) {
|
if let Some(_) = self.find_inode_id(name, &inode) {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
println!("stop1");
|
|
||||||
|
|
||||||
// create a new file
|
// create a new file
|
||||||
// alloc a inode with an indirect block
|
// alloc a inode with an indirect block
|
||||||
let new_inode_id = fs.alloc_inode();
|
let new_inode_id = fs.alloc_inode();
|
||||||
let indirect1 = fs.alloc_data();
|
let indirect1 = fs.alloc_data();
|
||||||
println!("creating new file, new_inode_id={}, indirect={}", new_inode_id, indirect1);
|
|
||||||
// initialize inode
|
// initialize inode
|
||||||
fs.get_disk_inode(new_inode_id).modify(|inode| {
|
fs.get_disk_inode(new_inode_id).modify(|inode| {
|
||||||
inode.initialize(
|
inode.initialize(
|
||||||
|
@ -133,7 +127,6 @@ impl Inode {
|
||||||
let file_count =
|
let file_count =
|
||||||
inode.read(|inode| inode.size as usize) / DIRENT_SZ;
|
inode.read(|inode| inode.size as usize) / DIRENT_SZ;
|
||||||
let new_size = (file_count + 1) * DIRENT_SZ;
|
let new_size = (file_count + 1) * DIRENT_SZ;
|
||||||
println!("expected new_size={}", new_size);
|
|
||||||
// increase size
|
// increase size
|
||||||
self.increase_size(new_size as u32, &mut inode, &mut fs);
|
self.increase_size(new_size as u32, &mut inode, &mut fs);
|
||||||
// write dirent
|
// write dirent
|
||||||
|
@ -155,13 +148,11 @@ impl Inode {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn ls(&self) -> Vec<String> {
|
pub fn ls(&self) -> Vec<String> {
|
||||||
println!("into ls!");
|
|
||||||
let mut fs = self.fs.lock();
|
let mut fs = self.fs.lock();
|
||||||
let inode = self.get_disk_inode(&mut fs);
|
let inode = self.get_disk_inode(&mut fs);
|
||||||
let file_count = inode.read(|inode| {
|
let file_count = inode.read(|inode| {
|
||||||
(inode.size as usize) / DIRENT_SZ
|
(inode.size as usize) / DIRENT_SZ
|
||||||
});
|
});
|
||||||
println!("file_count = {}", file_count);
|
|
||||||
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_bytes: DirentBytes = Default::default();
|
||||||
|
|
|
@ -17,6 +17,7 @@ virtio-drivers = { git = "https://github.com/rcore-os/virtio-drivers" }
|
||||||
k210-pac = { git = "https://github.com/wyfcyx/k210-pac" }
|
k210-pac = { git = "https://github.com/wyfcyx/k210-pac" }
|
||||||
k210-hal = { git = "https://github.com/wyfcyx/k210-hal" }
|
k210-hal = { git = "https://github.com/wyfcyx/k210-hal" }
|
||||||
k210-soc = { git = "https://github.com/wyfcyx/k210-soc" }
|
k210-soc = { git = "https://github.com/wyfcyx/k210-soc" }
|
||||||
|
easy-fs = { path = "../easy-fs" }
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
board_qemu = []
|
board_qemu = []
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue