From 08c8d89974cd8e027cb61470abd5b925c7b6ecb2 Mon Sep 17 00:00:00 2001 From: WangRunji Date: Fri, 22 Feb 2019 17:10:07 +0800 Subject: [PATCH] update SFS to new rcore-fs --- kernel/Cargo.lock | 45 +++++++++++++++----------- kernel/Cargo.toml | 3 +- kernel/src/drivers/block/virtio_blk.rs | 8 ++--- kernel/src/fs/device.rs | 24 ++++++++------ kernel/src/fs/file.rs | 6 ++-- kernel/src/fs/mod.rs | 5 +-- kernel/src/fs/stdio.rs | 6 ++-- kernel/src/process/structs.rs | 2 +- kernel/src/syscall/fs.rs | 9 ++++-- kernel/src/syscall/mod.rs | 6 ++-- kernel/src/syscall/proc.rs | 2 +- 11 files changed, 68 insertions(+), 48 deletions(-) diff --git a/kernel/Cargo.lock b/kernel/Cargo.lock index 645dc41..3c5fc27 100644 --- a/kernel/Cargo.lock +++ b/kernel/Cargo.lock @@ -61,11 +61,6 @@ dependencies = [ "bit_field 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "bit-vec" -version = "0.5.0" -source = "git+https://github.com/AltSysrq/bit-vec.git#9861a58d6e761906cc63964c759e71f05bf63540" - [[package]] name = "bit_field" version = "0.9.0" @@ -76,6 +71,11 @@ name = "bitflags" version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "bitvec" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "bootloader" version = "0.3.4" @@ -255,10 +255,11 @@ dependencies = [ "once 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", "pc-keyboard 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", "raw-cpuid 6.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "rcore-fs 0.1.0 (git+https://github.com/wangrunji0408/rcore-fs?branch=sefs)", + "rcore-fs-sfs 0.1.0 (git+https://github.com/wangrunji0408/rcore-fs?branch=sefs)", "rcore-memory 0.1.0", "rcore-thread 0.1.0", "riscv 0.3.0 (git+https://github.com/riscv-and-rust-and-decaf/riscv)", - "simple-filesystem 0.1.0 (git+https://github.com/wangrunji0408/SimpleFileSystem-Rust)", "smoltcp 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", "spin 0.4.10 (registry+https://github.com/rust-lang/crates.io-index)", "uart_16550 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -267,6 +268,23 @@ dependencies = [ "xmas-elf 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "rcore-fs" +version = "0.1.0" +source = "git+https://github.com/wangrunji0408/rcore-fs?branch=sefs#379a58cd6d5854db31d03c38ddf688adf8c774ed" + +[[package]] +name = "rcore-fs-sfs" +version = "0.1.0" +source = "git+https://github.com/wangrunji0408/rcore-fs?branch=sefs#379a58cd6d5854db31d03c38ddf688adf8c774ed" +dependencies = [ + "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)", + "rcore-fs 0.1.0 (git+https://github.com/wangrunji0408/rcore-fs?branch=sefs)", + "spin 0.4.10 (registry+https://github.com/rust-lang/crates.io-index)", + "static_assertions 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "rcore-memory" version = "0.1.0" @@ -338,16 +356,6 @@ name = "semver-parser" version = "0.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" -[[package]] -name = "simple-filesystem" -version = "0.1.0" -source = "git+https://github.com/wangrunji0408/SimpleFileSystem-Rust#48b3c2618a9589490a2b327844740a9fe1950b11" -dependencies = [ - "bit-vec 0.5.0 (git+https://github.com/AltSysrq/bit-vec.git)", - "spin 0.4.10 (registry+https://github.com/rust-lang/crates.io-index)", - "static_assertions 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", -] - [[package]] name = "skeptic" version = "0.5.0" @@ -491,9 +499,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum apic 0.1.0 (git+https://github.com/wangrunji0408/APIC-Rust)" = "" "checksum array-init 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "23589ecb866b460d3a0f1278834750268c607e8e28a1b982c907219f3178cd72" "checksum bare-metal 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)" = "a3caf393d93b2d453e80638d0674597020cef3382ada454faacd43d1a55a735a" -"checksum bit-vec 0.5.0 (git+https://github.com/AltSysrq/bit-vec.git)" = "" "checksum bit_field 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ed8765909f9009617974ab6b7d332625b320b33c326b1e9321382ef1999b5d56" "checksum bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "228047a76f468627ca71776ecdebd732a3423081fcf5125585bcd7c49886ce12" +"checksum bitvec 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "cfadef5c4e2c2e64067b9ecc061179f12ac7ec65ba613b1f60f3972bbada1f5b" "checksum bootloader 0.3.4 (git+https://github.com/wangrunji0408/bootloader)" = "" "checksum byteorder 1.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "a019b10a2a7cdeb292db131fc8113e57ea2a908f6e7894b0c3c671893b65dbeb" "checksum cc 1.0.28 (registry+https://github.com/rust-lang/crates.io-index)" = "bb4a8b715cb4597106ea87c7c84b2f1d452c7492033765df7f32651e66fcf749" @@ -517,6 +525,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum rand_core 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7a6fdeb83b075e8266dcc8762c22776f6877a63111121f5f8c7411e5be7eed4b" "checksum rand_core 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d0e7a549d590831370895ab7ba4ea0c1b6b011d106b5ff2da6eee112615e6dc0" "checksum raw-cpuid 6.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "30a9d219c32c9132f7be513c18be77c9881c7107d2ab5569d205a6a0f0e6dc7d" +"checksum rcore-fs 0.1.0 (git+https://github.com/wangrunji0408/rcore-fs?branch=sefs)" = "" +"checksum rcore-fs-sfs 0.1.0 (git+https://github.com/wangrunji0408/rcore-fs?branch=sefs)" = "" "checksum rdrand 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "678054eb77286b51581ba43620cc911abf02758c91f93f479767aed0f90458b2" "checksum register 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "e10f31b6d2299e5620986ad9fcdd66463e125ad72af4f403f9aedf7592d5ccdb" "checksum remove_dir_all 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "3488ba1b9a2084d38645c4c08276a1752dcbf2c7130d74f1569681ad5d2799c5" @@ -524,7 +534,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum rustc_version 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "138e3e0acb6c9fb258b19b67cb8abd63c00679d2851805ea151465464fe9030a" "checksum semver 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "1d7eb9ef2c18661902cc47e535f9bc51b78acd254da71d375c2f6720d9a40403" "checksum semver-parser 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3" -"checksum simple-filesystem 0.1.0 (git+https://github.com/wangrunji0408/SimpleFileSystem-Rust)" = "" "checksum skeptic 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "061203a849117b0f7090baf8157aa91dac30545208fbb85166ac58b4ca33d89c" "checksum smoltcp 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fef582369edb298c6c41319a544ca9c4e83622f226055ccfcb35974fbb55ed34" "checksum spin 0.4.10 (registry+https://github.com/rust-lang/crates.io-index)" = "ceac490aa12c567115b40b7b7fceca03a6c9d53d5defea066123debc83c5dc1f" diff --git a/kernel/Cargo.toml b/kernel/Cargo.toml index 0bb1355..e29f98e 100644 --- a/kernel/Cargo.toml +++ b/kernel/Cargo.toml @@ -55,7 +55,8 @@ smoltcp = { version = "0.5.0", default-features = false, features = ["alloc", "l bit-allocator = { path = "../crate/bit-allocator" } rcore-memory = { path = "../crate/memory" } rcore-thread = { path = "../crate/thread" } -simple-filesystem = { git = "https://github.com/wangrunji0408/SimpleFileSystem-Rust" } +rcore-fs = { git = "https://github.com/wangrunji0408/rcore-fs", branch = "sefs" } +rcore-fs-sfs = { git = "https://github.com/wangrunji0408/rcore-fs", branch = "sefs" } [target.'cfg(target_arch = "x86_64")'.dependencies] bootloader = { git = "https://github.com/wangrunji0408/bootloader" } diff --git a/kernel/src/drivers/block/virtio_blk.rs b/kernel/src/drivers/block/virtio_blk.rs index d373c66..4cebfdc 100644 --- a/kernel/src/drivers/block/virtio_blk.rs +++ b/kernel/src/drivers/block/virtio_blk.rs @@ -14,7 +14,7 @@ use rcore_memory::PAGE_SIZE; use rcore_memory::paging::PageTable; use volatile::Volatile; -use simple_filesystem::BlockedDevice; +use rcore_fs::dev::BlockDevice; use crate::arch::cpu; use crate::memory::active_table; @@ -115,9 +115,9 @@ impl Driver for VirtIOBlkDriver { } } -impl BlockedDevice for VirtIOBlkDriver { +impl BlockDevice for VirtIOBlkDriver { const BLOCK_SIZE_LOG2: u8 = 9; // 512 - fn read_at(&mut self, block_id: usize, buf: &mut [u8]) -> bool { + fn read_at(&self, block_id: usize, buf: &mut [u8]) -> bool { let mut driver = self.0.lock(); // ensure header page is mapped active_table().map_if_not_exists(driver.header as usize, driver.header as usize); @@ -140,7 +140,7 @@ impl BlockedDevice for VirtIOBlkDriver { } } - fn write_at(&mut self, block_id: usize, buf: &[u8]) -> bool { + fn write_at(&self, block_id: usize, buf: &[u8]) -> bool { unimplemented!() } } diff --git a/kernel/src/fs/device.rs b/kernel/src/fs/device.rs index 9f0feb3..3afd8e3 100644 --- a/kernel/src/fs/device.rs +++ b/kernel/src/fs/device.rs @@ -1,44 +1,48 @@ //! Implement Device -use simple_filesystem::*; +use spin::RwLock; +use rcore_fs::dev::*; #[cfg(target_arch = "x86_64")] use crate::arch::driver::ide; #[cfg(not(target_arch = "x86_64"))] -pub struct MemBuf(&'static [u8]); +pub struct MemBuf(RwLock<&'static mut [u8]>); #[cfg(not(target_arch = "x86_64"))] impl MemBuf { unsafe fn new(begin: unsafe extern fn(), end: unsafe extern fn()) -> Self { use core::slice; - MemBuf(slice::from_raw_parts(begin as *const u8, end as usize - begin as usize)) + MemBuf(RwLock::new(slice::from_raw_parts_mut(begin as *mut u8, end as usize - begin as usize))) } } #[cfg(not(target_arch = "x86_64"))] impl Device for MemBuf { - fn read_at(&mut self, offset: usize, buf: &mut [u8]) -> Option { - let slice = self.0; + fn read_at(&self, offset: usize, buf: &mut [u8]) -> Option { + let slice = self.0.read(); let len = buf.len().min(slice.len() - offset); buf[..len].copy_from_slice(&slice[offset..offset + len]); Some(len) } - fn write_at(&mut self, _offset: usize, _buf: &[u8]) -> Option { - None + fn write_at(&self, offset: usize, buf: &[u8]) -> Option { + let mut slice = self.0.write(); + let len = buf.len().min(slice.len() - offset); + slice[offset..offset + len].copy_from_slice(&buf[..len]); + Some(len) } } #[cfg(target_arch = "x86_64")] -impl BlockedDevice for ide::IDE { +impl BlockDevice for ide::IDE { const BLOCK_SIZE_LOG2: u8 = 9; - fn read_at(&mut self, block_id: usize, buf: &mut [u8]) -> bool { + fn read_at(&self, block_id: usize, buf: &mut [u8]) -> bool { use core::slice; assert!(buf.len() >= ide::BLOCK_SIZE); let buf = 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() } - fn write_at(&mut self, block_id: usize, buf: &[u8]) -> bool { + fn write_at(&self, block_id: usize, buf: &[u8]) -> bool { use core::slice; assert!(buf.len() >= ide::BLOCK_SIZE); let buf = unsafe { slice::from_raw_parts(buf.as_ptr() as *mut u32, ide::BLOCK_SIZE / 4) }; diff --git a/kernel/src/fs/file.rs b/kernel/src/fs/file.rs index d7d4d4f..9658bb9 100644 --- a/kernel/src/fs/file.rs +++ b/kernel/src/fs/file.rs @@ -2,7 +2,7 @@ use alloc::{string::String, sync::Arc}; -use simple_filesystem::{FileInfo, INode, Result}; +use rcore_fs::vfs::{Metadata, INode, Result}; #[derive(Clone)] pub struct File { @@ -31,8 +31,8 @@ impl File { Ok(len) } - pub fn info(&self) -> Result { - self.inode.info() + pub fn info(&self) -> Result { + self.inode.metadata() } pub fn get_entry(&self, id: usize) -> Result { diff --git a/kernel/src/fs/mod.rs b/kernel/src/fs/mod.rs index 078bf2e..22aa44c 100644 --- a/kernel/src/fs/mod.rs +++ b/kernel/src/fs/mod.rs @@ -2,7 +2,8 @@ use alloc::{boxed::Box, collections::VecDeque, string::String, sync::Arc, vec::V use core::any::Any; use core::ops::Deref; -use simple_filesystem::*; +use rcore_fs::vfs::*; +use rcore_fs_sfs::SimpleFileSystem; #[cfg(target_arch = "x86_64")] use crate::arch::driver::ide; @@ -54,7 +55,7 @@ pub trait INodeExt { impl INodeExt for INode { fn read_as_vec(&self) -> Result> { - let size = self.info()?.size; + let size = self.metadata()?.size; let mut buf = Vec::with_capacity(size); unsafe { buf.set_len(size); } self.read_at(0, buf.as_mut_slice())?; diff --git a/kernel/src/fs/stdio.rs b/kernel/src/fs/stdio.rs index 022f018..184243a 100644 --- a/kernel/src/fs/stdio.rs +++ b/kernel/src/fs/stdio.rs @@ -3,7 +3,7 @@ use alloc::{collections::vec_deque::VecDeque, string::String, sync::Arc}; use core::any::Any; -use simple_filesystem::*; +use rcore_fs::vfs::*; use crate::sync::Condvar; use crate::sync::SpinNoIrqLock as Mutex; @@ -49,10 +49,10 @@ lazy_static! { // TODO: better way to provide default impl? macro_rules! impl_inode { () => { - fn info(&self) -> Result { Err(FsError::NotSupported) } + fn metadata(&self) -> Result { Err(FsError::NotSupported) } fn sync(&self) -> Result<()> { Ok(()) } fn resize(&self, _len: usize) -> Result<()> { Err(FsError::NotSupported) } - fn create(&self, _name: &str, _type_: FileType) -> Result> { Err(FsError::NotDir) } + fn create(&self, _name: &str, _type_: FileType, _mode: u32) -> Result> { Err(FsError::NotDir) } fn unlink(&self, _name: &str) -> Result<()> { Err(FsError::NotDir) } fn link(&self, _name: &str, _other: &Arc) -> Result<()> { Err(FsError::NotDir) } fn rename(&self, _old_name: &str, _new_name: &str) -> Result<()> { Err(FsError::NotDir) } diff --git a/kernel/src/process/structs.rs b/kernel/src/process/structs.rs index 58de0c3..2248af6 100644 --- a/kernel/src/process/structs.rs +++ b/kernel/src/process/structs.rs @@ -1,7 +1,7 @@ use alloc::{boxed::Box, collections::BTreeMap, string::String, sync::Arc, vec::Vec}; use log::*; -use simple_filesystem::INode; +use rcore_fs::vfs::INode; use spin::Mutex; use xmas_elf::{ElfFile, header, program::{Flags, Type}}; diff --git a/kernel/src/syscall/fs.rs b/kernel/src/syscall/fs.rs index 6c8f1a1..6a8889c 100644 --- a/kernel/src/syscall/fs.rs +++ b/kernel/src/syscall/fs.rs @@ -173,13 +173,16 @@ bitflags! { } } -impl From for Stat { - fn from(info: FileInfo) -> Self { +impl From for Stat { + fn from(info: Metadata) -> Self { Stat { mode: match info.type_ { FileType::File => StatMode::FILE, FileType::Dir => StatMode::DIR, - // _ => StatMode::NULL, + FileType::SymLink => StatMode::LINK, + FileType::CharDevice => StatMode::CHAR, + FileType::BlockDevice => StatMode::BLOCK, + _ => StatMode::NULL, //Note: we should mark FileType as #[non_exhaustive] // but it is currently not implemented for enum // see rust-lang/rust#44109 diff --git a/kernel/src/syscall/mod.rs b/kernel/src/syscall/mod.rs index 7f1e2ab..d3b0e8a 100644 --- a/kernel/src/syscall/mod.rs +++ b/kernel/src/syscall/mod.rs @@ -4,8 +4,7 @@ use alloc::{string::String, sync::Arc, vec::Vec}; use core::{slice, str}; use bitflags::bitflags; -use log::*; -use simple_filesystem::{FileInfo, FileType, FsError, INode}; +use rcore_fs::vfs::{Metadata, FileType, FsError, INode}; use spin::{Mutex, MutexGuard}; use crate::arch::interrupt::TrapFrame; @@ -110,6 +109,7 @@ pub type SysResult = Result; #[repr(isize)] #[derive(Debug)] pub enum SysError { + // TODO: Linux Error Code // ucore compatible error code // note that ucore_plus use another error code table, which is a modified version of the ones used in linux // name conversion E_XXXXX -> SysError::Xxxxx @@ -124,6 +124,7 @@ pub enum SysError { Unimp = 20,// Not implemented Exists = 23,// File exists Notempty = 24,// Directory is not empty + Io = 5,// I/O Error #[allow(dead_code)] Unspcified = 1,// A really really unknown error. @@ -144,6 +145,7 @@ impl From for SysError { FsError::DirRemoved => SysError::Noent, FsError::DirNotEmpty => SysError::Notempty, FsError::WrongFs => SysError::Inval, + FsError::DeviceError => SysError::Io, } } } diff --git a/kernel/src/syscall/proc.rs b/kernel/src/syscall/proc.rs index 7810577..c25a9eb 100644 --- a/kernel/src/syscall/proc.rs +++ b/kernel/src/syscall/proc.rs @@ -65,7 +65,7 @@ pub fn sys_exec(name: *const u8, argc: usize, argv: *const *const u8, tf: &mut T // Read program file let path = args[0].as_str(); let inode = crate::fs::ROOT_INODE.lookup(path)?; - let size = inode.info()?.size; + let size = inode.metadata()?.size; let mut buf = Vec::with_capacity(size); unsafe { buf.set_len(size); } inode.read_at(0, buf.as_mut_slice())?;