update fs. add BlockCache

toolchain_update
WangRunji 6 years ago
parent feae733bb9
commit 6883127d5a

7
kernel/Cargo.lock generated

@ -396,12 +396,15 @@ dependencies = [
[[package]] [[package]]
name = "rcore-fs" name = "rcore-fs"
version = "0.1.0" version = "0.1.0"
source = "git+https://github.com/rcore-os/rcore-fs#6f282baf2fe928d2c9774e526e63125684855221" source = "git+https://github.com/rcore-os/rcore-fs#41ccb1675cbea1df079f39fdc1bcd50c609df707"
dependencies = [
"spin 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]] [[package]]
name = "rcore-fs-sfs" name = "rcore-fs-sfs"
version = "0.1.0" version = "0.1.0"
source = "git+https://github.com/rcore-os/rcore-fs#6f282baf2fe928d2c9774e526e63125684855221" source = "git+https://github.com/rcore-os/rcore-fs#41ccb1675cbea1df079f39fdc1bcd50c609df707"
dependencies = [ dependencies = [
"bitvec 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)", "bitvec 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",

@ -48,8 +48,6 @@ pub fn init(_irq: Option<u32>, header: usize, size: usize) -> Arc<AHCIDriver> {
let ahci = AHCI::new(header, size); let ahci = AHCI::new(header, size);
let driver = Arc::new(AHCIDriver(Mutex::new(ahci))); let driver = Arc::new(AHCIDriver(Mutex::new(ahci)));
DRIVERS.write().push(driver.clone()); DRIVERS.write().push(driver.clone());
BLK_DRIVERS BLK_DRIVERS.write().push(driver.clone());
.write()
.push(Arc::new(BlockDriver(driver.clone())));
driver driver
} }

@ -222,5 +222,5 @@ pub fn virtio_blk_init(node: &Node) {
let driver = Arc::new(driver); let driver = Arc::new(driver);
DRIVERS.write().push(driver.clone()); DRIVERS.write().push(driver.clone());
BLK_DRIVERS.write().push(Arc::new(BlockDriver(driver))); BLK_DRIVERS.write().push(driver);
} }

@ -7,7 +7,7 @@ use smoltcp::wire::{EthernetAddress, IpAddress, IpCidr, Ipv4Address};
use spin::RwLock; use spin::RwLock;
use crate::sync::Condvar; use crate::sync::Condvar;
use rcore_fs::dev::BlockDevice; use rcore_fs::dev::{self, BlockDevice, DevError};
#[allow(dead_code)] #[allow(dead_code)]
pub mod block; pub mod block;
@ -95,19 +95,29 @@ lazy_static! {
// NOTE: RwLock only write when initializing drivers // NOTE: RwLock only write when initializing drivers
pub static ref DRIVERS: RwLock<Vec<Arc<Driver>>> = RwLock::new(Vec::new()); pub static ref DRIVERS: RwLock<Vec<Arc<Driver>>> = RwLock::new(Vec::new());
pub static ref NET_DRIVERS: RwLock<Vec<Arc<Driver>>> = RwLock::new(Vec::new()); pub static ref NET_DRIVERS: RwLock<Vec<Arc<Driver>>> = RwLock::new(Vec::new());
pub static ref BLK_DRIVERS: RwLock<Vec<Arc<BlockDriver>>> = RwLock::new(Vec::new()); pub static ref BLK_DRIVERS: RwLock<Vec<Arc<Driver>>> = RwLock::new(Vec::new());
} }
pub struct BlockDriver(Arc<Driver>); pub struct BlockDriver(pub Arc<Driver>);
impl BlockDevice for BlockDriver { impl BlockDevice for BlockDriver {
const BLOCK_SIZE_LOG2: u8 = 9; // 512 const BLOCK_SIZE_LOG2: u8 = 9; // 512
fn read_at(&self, block_id: usize, buf: &mut [u8]) -> bool { fn read_at(&self, block_id: usize, buf: &mut [u8]) -> dev::Result<()> {
self.0.read_block(block_id, buf) match self.0.read_block(block_id, buf) {
true => Ok(()),
false => Err(DevError),
}
} }
fn write_at(&self, block_id: usize, buf: &[u8]) -> bool { fn write_at(&self, block_id: usize, buf: &[u8]) -> dev::Result<()> {
self.0.write_block(block_id, buf) match self.0.write_block(block_id, buf) {
true => Ok(()),
false => Err(DevError),
}
}
fn sync(&self) -> dev::Result<()> {
Ok(())
} }
} }

@ -19,34 +19,42 @@ impl MemBuf {
} }
impl Device for MemBuf { impl Device for MemBuf {
fn read_at(&self, offset: usize, buf: &mut [u8]) -> Option<usize> { fn read_at(&self, offset: usize, buf: &mut [u8]) -> Result<usize> {
let slice = self.0.read(); let slice = self.0.read();
let len = buf.len().min(slice.len() - offset); let len = buf.len().min(slice.len() - offset);
buf[..len].copy_from_slice(&slice[offset..offset + len]); buf[..len].copy_from_slice(&slice[offset..offset + len]);
Some(len) Ok(len)
} }
fn write_at(&self, offset: usize, buf: &[u8]) -> Option<usize> { fn write_at(&self, offset: usize, buf: &[u8]) -> Result<usize> {
let mut slice = self.0.write(); let mut slice = self.0.write();
let len = buf.len().min(slice.len() - offset); let len = buf.len().min(slice.len() - offset);
slice[offset..offset + len].copy_from_slice(&buf[..len]); slice[offset..offset + len].copy_from_slice(&buf[..len]);
Some(len) Ok(len)
}
fn sync(&self) -> Result<()> {
Ok(())
} }
} }
#[cfg(target_arch = "x86_64")] #[cfg(target_arch = "x86_64")]
impl BlockDevice for ide::IDE { impl BlockDevice for ide::IDE {
const BLOCK_SIZE_LOG2: u8 = 9; const BLOCK_SIZE_LOG2: u8 = 9;
fn read_at(&self, block_id: usize, buf: &mut [u8]) -> bool { fn read_at(&self, block_id: usize, buf: &mut [u8]) -> Result<()> {
use core::slice; use core::slice;
assert!(buf.len() >= ide::BLOCK_SIZE); assert!(buf.len() >= ide::BLOCK_SIZE);
let buf = let buf =
unsafe { slice::from_raw_parts_mut(buf.as_ptr() as *mut u32, ide::BLOCK_SIZE / 4) }; unsafe { slice::from_raw_parts_mut(buf.as_ptr() as *mut u32, ide::BLOCK_SIZE / 4) };
self.read(block_id as u64, 1, buf).is_ok() self.read(block_id as u64, 1, buf).map_err(|_| DevError)?;
Ok(())
} }
fn write_at(&self, block_id: usize, buf: &[u8]) -> bool { fn write_at(&self, block_id: usize, buf: &[u8]) -> Result<()> {
use core::slice; use core::slice;
assert!(buf.len() >= ide::BLOCK_SIZE); assert!(buf.len() >= ide::BLOCK_SIZE);
let buf = unsafe { slice::from_raw_parts(buf.as_ptr() as *mut u32, ide::BLOCK_SIZE / 4) }; let buf = unsafe { slice::from_raw_parts(buf.as_ptr() as *mut u32, ide::BLOCK_SIZE / 4) };
self.write(block_id as u64, 1, buf).is_ok() self.write(block_id as u64, 1, buf).map_err(|_| DevError)?;
Ok(())
}
fn sync(&self) -> Result<()> {
Ok(())
} }
} }

@ -1,8 +1,11 @@
use alloc::{sync::Arc, vec::Vec}; use alloc::{sync::Arc, vec::Vec};
use rcore_fs::vfs::*; use rcore_fs::vfs::*;
use rcore_fs::dev::block_cache::BlockCache;
use rcore_fs_sfs::SimpleFileSystem; use rcore_fs_sfs::SimpleFileSystem;
use crate::drivers::BlockDriver;
pub use self::file::*; pub use self::file::*;
pub use self::file_like::*; pub use self::file_like::*;
pub use self::pipe::Pipe; pub use self::pipe::Pipe;
@ -38,9 +41,15 @@ lazy_static! {
let device = { let device = {
#[cfg(any(target_arch = "riscv32", target_arch = "riscv64", target_arch = "x86_64"))] #[cfg(any(target_arch = "riscv32", target_arch = "riscv64", target_arch = "x86_64"))]
{ {
crate::drivers::BLK_DRIVERS.read().iter() let driver = BlockDriver(
.next().expect("Block device not found") crate::drivers::BLK_DRIVERS
.clone() .read().iter()
.next().expect("Block device not found")
.clone()
);
// enable block cache
// Arc::new(BlockCache::new(driver, 0x100))
Arc::new(driver)
} }
#[cfg(target_arch = "aarch64")] #[cfg(target_arch = "aarch64")]
{ {

@ -1 +1 @@
Subproject commit aec8667eb056cb564e301d6c0937da1e4500a4d3 Subproject commit b6a347750531be125583107b6d0fc307366fdcc9
Loading…
Cancel
Save