|
|
@ -1,11 +1,13 @@
|
|
|
|
//! C Interfaces for ucore
|
|
|
|
//! C Interfaces for ucore
|
|
|
|
|
|
|
|
|
|
|
|
use alloc::{rc::Rc, boxed::Box};
|
|
|
|
use alloc::{rc::Rc, boxed::Box, BTreeMap};
|
|
|
|
use core::cell::RefCell;
|
|
|
|
use core::cell::RefCell;
|
|
|
|
use core::slice;
|
|
|
|
use core::slice;
|
|
|
|
|
|
|
|
use core::ops::Deref;
|
|
|
|
/// Global allocator defined in root
|
|
|
|
use core::cmp::Ordering;
|
|
|
|
pub use self::allocator::UcoreAllocator;
|
|
|
|
use alloc::heap::{Alloc, AllocErr, Layout};
|
|
|
|
|
|
|
|
use core::mem::size_of;
|
|
|
|
|
|
|
|
use vfs;
|
|
|
|
|
|
|
|
|
|
|
|
/// Lang items for bare lib
|
|
|
|
/// Lang items for bare lib
|
|
|
|
mod lang {
|
|
|
|
mod lang {
|
|
|
@ -31,6 +33,8 @@ mod lang {
|
|
|
|
mod ucore {
|
|
|
|
mod ucore {
|
|
|
|
use super::*;
|
|
|
|
use super::*;
|
|
|
|
extern {
|
|
|
|
extern {
|
|
|
|
|
|
|
|
pub fn kmalloc(size: usize) -> *mut u8;
|
|
|
|
|
|
|
|
pub fn kfree(ptr: *mut u8);
|
|
|
|
pub fn inode_kill(inode: &mut INode);
|
|
|
|
pub fn inode_kill(inode: &mut INode);
|
|
|
|
pub fn create_inode_for_sfs(ops: &INodeOps, fs: &mut Fs) -> *mut INode;
|
|
|
|
pub fn create_inode_for_sfs(ops: &INodeOps, fs: &mut Fs) -> *mut INode;
|
|
|
|
pub fn create_fs_for_sfs(ops: &FsOps) -> *mut Fs;
|
|
|
|
pub fn create_fs_for_sfs(ops: &FsOps) -> *mut Fs;
|
|
|
@ -46,13 +50,10 @@ macro_rules! cprintf {
|
|
|
|
|
|
|
|
|
|
|
|
// Exports for ucore
|
|
|
|
// Exports for ucore
|
|
|
|
|
|
|
|
|
|
|
|
static SFS_INODE_OPS: INodeOps = INodeOps::from_rust_inode::<sfs::INode>();
|
|
|
|
|
|
|
|
static SFS_FS_OPS: FsOps = FsOps::from_rust_fs::<sfs::SimpleFileSystem>();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[no_mangle]
|
|
|
|
#[no_mangle]
|
|
|
|
pub extern fn sfs_do_mount(dev: *mut Device, fs_store: &mut *mut Fs) -> ErrorCode {
|
|
|
|
pub extern fn sfs_do_mount(dev: *mut Device, fs_store: &mut *mut Fs) -> ErrorCode {
|
|
|
|
use self::ucore::*;
|
|
|
|
use sfs;
|
|
|
|
let fs = unsafe{create_fs_for_sfs(&SFS_FS_OPS)};
|
|
|
|
let fs = unsafe{ ucore::create_fs_for_sfs(&FS_OPS) };
|
|
|
|
debug_assert!(!dev.is_null());
|
|
|
|
debug_assert!(!dev.is_null());
|
|
|
|
let mut device = unsafe{ Box::from_raw(dev) }; // TODO: fix unsafe
|
|
|
|
let mut device = unsafe{ Box::from_raw(dev) }; // TODO: fix unsafe
|
|
|
|
device.open();
|
|
|
|
device.open();
|
|
|
@ -68,7 +69,7 @@ pub extern fn sfs_do_mount(dev: *mut Device, fs_store: &mut *mut Fs) -> ErrorCod
|
|
|
|
/// Match struct `inode` in ucore `kern/fs/vfs/inode.h`
|
|
|
|
/// Match struct `inode` in ucore `kern/fs/vfs/inode.h`
|
|
|
|
#[repr(C)]
|
|
|
|
#[repr(C)]
|
|
|
|
struct INode {
|
|
|
|
struct INode {
|
|
|
|
inode: Rc<RefCell<vfs::INode>>,
|
|
|
|
inode: vfs::INodePtr,
|
|
|
|
// ... fields handled extern
|
|
|
|
// ... fields handled extern
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -249,9 +250,6 @@ pub enum ErrorCode {
|
|
|
|
|
|
|
|
|
|
|
|
// Wrapper functions
|
|
|
|
// Wrapper functions
|
|
|
|
|
|
|
|
|
|
|
|
use vfs;
|
|
|
|
|
|
|
|
use sfs;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
impl AsRef<[u8]> for IoBuf {
|
|
|
|
impl AsRef<[u8]> for IoBuf {
|
|
|
|
fn as_ref(&self) -> &[u8] {
|
|
|
|
fn as_ref(&self) -> &[u8] {
|
|
|
|
unsafe{ slice::from_raw_parts(self.base, self.resident as usize) }
|
|
|
|
unsafe{ slice::from_raw_parts(self.base, self.resident as usize) }
|
|
|
@ -332,15 +330,28 @@ impl Device {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl INode {
|
|
|
|
impl INode {
|
|
|
|
fn new(fs: &mut Fs) -> &mut Self {
|
|
|
|
fn get_or_create(vfs_inode: vfs::INodePtr, fs: &mut Fs) -> *mut Self {
|
|
|
|
use self::ucore::*;
|
|
|
|
static mut MAPPER: *mut BTreeMap<usize, *mut INode> = 0 as *mut _;
|
|
|
|
let inode = unsafe{ create_inode_for_sfs(&SFS_INODE_OPS, fs) };
|
|
|
|
|
|
|
|
|
|
|
|
unsafe {if MAPPER.is_null() {
|
|
|
|
|
|
|
|
MAPPER = Box::into_raw(Box::new(BTreeMap::<usize, *mut INode>::new()));
|
|
|
|
|
|
|
|
} }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let mapper = unsafe{ &mut *MAPPER };
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let addr = vfs_inode.as_ptr() as *const () as usize;
|
|
|
|
|
|
|
|
match mapper.get(&addr) {
|
|
|
|
|
|
|
|
Some(&ptr) => ptr,
|
|
|
|
|
|
|
|
None => {
|
|
|
|
|
|
|
|
let inode = unsafe{ ucore::create_inode_for_sfs(&INODE_OPS, fs) };
|
|
|
|
assert!(!inode.is_null());
|
|
|
|
assert!(!inode.is_null());
|
|
|
|
unsafe{ &mut *inode }
|
|
|
|
mapper.insert(addr, inode);
|
|
|
|
|
|
|
|
inode
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fn drop(&mut self) {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
use self::ucore::*;
|
|
|
|
unsafe{ ucore::inode_kill(self) };
|
|
|
|
unsafe{ inode_kill(self) };
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -355,8 +366,15 @@ impl From<vfs::FileInfo> for Stat {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl INodeOps {
|
|
|
|
static INODE_OPS: INodeOps = {
|
|
|
|
const fn from_rust_inode<T: vfs::INode>() -> Self {
|
|
|
|
impl Deref for INode {
|
|
|
|
|
|
|
|
type Target = vfs::INodePtr;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
|
|
|
|
&self.inode
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
extern fn open(inode: &mut INode, flags: u32) -> ErrorCode {
|
|
|
|
extern fn open(inode: &mut INode, flags: u32) -> ErrorCode {
|
|
|
|
ErrorCode::Unimplemented
|
|
|
|
ErrorCode::Unimplemented
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -364,25 +382,22 @@ impl INodeOps {
|
|
|
|
ErrorCode::Unimplemented
|
|
|
|
ErrorCode::Unimplemented
|
|
|
|
}
|
|
|
|
}
|
|
|
|
extern fn read(inode: &mut INode, buf: &mut IoBuf) -> ErrorCode {
|
|
|
|
extern fn read(inode: &mut INode, buf: &mut IoBuf) -> ErrorCode {
|
|
|
|
let inode = &inode.inode;
|
|
|
|
|
|
|
|
let len = inode.borrow().read_at(buf.offset as usize, buf.as_mut()).unwrap();
|
|
|
|
let len = inode.borrow().read_at(buf.offset as usize, buf.as_mut()).unwrap();
|
|
|
|
buf.skip(len);
|
|
|
|
buf.skip(len);
|
|
|
|
ErrorCode::Ok
|
|
|
|
ErrorCode::Ok
|
|
|
|
}
|
|
|
|
}
|
|
|
|
extern fn write(inode: &mut INode, buf: &mut IoBuf) -> ErrorCode {
|
|
|
|
extern fn write(inode: &mut INode, buf: &mut IoBuf) -> ErrorCode {
|
|
|
|
let inode = &inode.inode;
|
|
|
|
|
|
|
|
let len = inode.borrow().write_at(buf.offset as usize, buf.as_ref()).unwrap();
|
|
|
|
let len = inode.borrow().write_at(buf.offset as usize, buf.as_ref()).unwrap();
|
|
|
|
buf.skip(len);
|
|
|
|
buf.skip(len);
|
|
|
|
ErrorCode::Ok
|
|
|
|
ErrorCode::Ok
|
|
|
|
}
|
|
|
|
}
|
|
|
|
extern fn fstat(inode: &mut INode, stat: &mut Stat) -> ErrorCode {
|
|
|
|
extern fn fstat(inode: &mut INode, stat: &mut Stat) -> ErrorCode {
|
|
|
|
let inode = &inode.inode;
|
|
|
|
|
|
|
|
let info = inode.borrow().info().unwrap();
|
|
|
|
let info = inode.borrow().info().unwrap();
|
|
|
|
*stat = Stat::from(info);
|
|
|
|
*stat = Stat::from(info);
|
|
|
|
ErrorCode::Ok
|
|
|
|
ErrorCode::Ok
|
|
|
|
}
|
|
|
|
}
|
|
|
|
extern fn fsync(inode: &mut INode) -> ErrorCode {
|
|
|
|
extern fn fsync(inode: &mut INode) -> ErrorCode {
|
|
|
|
inode.inode.borrow_mut().sync().unwrap();
|
|
|
|
inode.borrow_mut().sync().unwrap();
|
|
|
|
ErrorCode::Ok
|
|
|
|
ErrorCode::Ok
|
|
|
|
}
|
|
|
|
}
|
|
|
|
extern fn namefile(inode: &mut INode, buf: &mut IoBuf) -> ErrorCode {
|
|
|
|
extern fn namefile(inode: &mut INode, buf: &mut IoBuf) -> ErrorCode {
|
|
|
@ -395,7 +410,6 @@ impl INodeOps {
|
|
|
|
ErrorCode::Unimplemented
|
|
|
|
ErrorCode::Unimplemented
|
|
|
|
}
|
|
|
|
}
|
|
|
|
extern fn gettype(inode: &mut INode, type_store: &mut u32) -> ErrorCode {
|
|
|
|
extern fn gettype(inode: &mut INode, type_store: &mut u32) -> ErrorCode {
|
|
|
|
let inode = &inode.inode;
|
|
|
|
|
|
|
|
let info = inode.borrow().info().unwrap();
|
|
|
|
let info = inode.borrow().info().unwrap();
|
|
|
|
*type_store = info.type_ as u32;
|
|
|
|
*type_store = info.type_ as u32;
|
|
|
|
ErrorCode::Ok
|
|
|
|
ErrorCode::Ok
|
|
|
@ -420,18 +434,26 @@ impl INodeOps {
|
|
|
|
open, close, read, write, fstat, fsync, namefile, getdirentry,
|
|
|
|
open, close, read, write, fstat, fsync, namefile, getdirentry,
|
|
|
|
reclaim, gettype, tryseek, truncate, create, lookup, ioctl,
|
|
|
|
reclaim, gettype, tryseek, truncate, create, lookup, ioctl,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static FS_OPS: FsOps = {
|
|
|
|
|
|
|
|
impl Deref for Fs {
|
|
|
|
|
|
|
|
type Target = Rc<vfs::FileSystem>;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
|
|
|
|
&self.fs
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
impl FsOps {
|
|
|
|
|
|
|
|
const fn from_rust_fs<T: vfs::FileSystem>() -> Self {
|
|
|
|
|
|
|
|
extern fn sync(fs: &mut Fs) -> ErrorCode {
|
|
|
|
extern fn sync(fs: &mut Fs) -> ErrorCode {
|
|
|
|
fs.fs.sync().unwrap();
|
|
|
|
cprintf!("fs.sync");
|
|
|
|
|
|
|
|
fs.sync().unwrap();
|
|
|
|
ErrorCode::Ok
|
|
|
|
ErrorCode::Ok
|
|
|
|
}
|
|
|
|
}
|
|
|
|
extern fn get_root(fs: &mut Fs) -> *mut INode {
|
|
|
|
extern fn get_root(fs: &mut Fs) -> *mut INode {
|
|
|
|
fs.fs.root_inode();
|
|
|
|
cprintf!("fs.getroot");
|
|
|
|
unimplemented!();
|
|
|
|
let inode = fs.root_inode();
|
|
|
|
|
|
|
|
INode::get_or_create(inode, fs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
extern fn unmount(fs: &mut Fs) -> ErrorCode {
|
|
|
|
extern fn unmount(fs: &mut Fs) -> ErrorCode {
|
|
|
|
unimplemented!();
|
|
|
|
unimplemented!();
|
|
|
@ -440,29 +462,20 @@ impl FsOps {
|
|
|
|
unimplemented!();
|
|
|
|
unimplemented!();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
FsOps { sync, get_root, unmount, cleanup }
|
|
|
|
FsOps { sync, get_root, unmount, cleanup }
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
mod allocator {
|
|
|
|
|
|
|
|
use alloc::heap::{Alloc, AllocErr, Layout};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
extern {
|
|
|
|
|
|
|
|
fn kmalloc(size: usize) -> *mut u8;
|
|
|
|
|
|
|
|
fn kfree(ptr: *mut u8);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub struct UcoreAllocator;
|
|
|
|
/// Allocator supported by ucore functions
|
|
|
|
|
|
|
|
pub struct UcoreAllocator;
|
|
|
|
|
|
|
|
|
|
|
|
unsafe impl<'a> Alloc for &'a UcoreAllocator {
|
|
|
|
unsafe impl<'a> Alloc for &'a UcoreAllocator {
|
|
|
|
unsafe fn alloc(&mut self, layout: Layout) -> Result<*mut u8, AllocErr> {
|
|
|
|
unsafe fn alloc(&mut self, layout: Layout) -> Result<*mut u8, AllocErr> {
|
|
|
|
const NULL: *mut u8 = 0 as *mut u8;
|
|
|
|
const NULL: *mut u8 = 0 as *mut u8;
|
|
|
|
match kmalloc(layout.size()) {
|
|
|
|
match ucore::kmalloc(layout.size()) {
|
|
|
|
NULL => Err(AllocErr::Exhausted { request: layout }),
|
|
|
|
NULL => Err(AllocErr::Exhausted { request: layout }),
|
|
|
|
ptr => Ok(ptr),
|
|
|
|
ptr => Ok(ptr),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
unsafe fn dealloc(&mut self, ptr: *mut u8, layout: Layout) {
|
|
|
|
unsafe fn dealloc(&mut self, ptr: *mut u8, layout: Layout) {
|
|
|
|
kfree(ptr);
|
|
|
|
ucore::kfree(ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|