diff --git a/kernel/scheme/debug.rs b/kernel/scheme/debug.rs index c3b77d8..a282191 100644 --- a/kernel/scheme/debug.rs +++ b/kernel/scheme/debug.rs @@ -23,18 +23,18 @@ pub extern fn debug_input(b: u8) { pub struct DebugScheme; impl Scheme for DebugScheme { - fn open(&mut self, _path: &[u8], _flags: usize) -> Result { + fn open(&self, _path: &[u8], _flags: usize) -> Result { Ok(0) } - fn dup(&mut self, _file: usize) -> Result { + fn dup(&self, _file: usize) -> Result { Ok(0) } /// Read the file `number` into the `buffer` /// /// Returns the number of bytes read - fn read(&mut self, _file: usize, buf: &mut [u8]) -> Result { + fn read(&self, _file: usize, buf: &mut [u8]) -> Result { loop { let mut i = 0; { @@ -56,18 +56,18 @@ impl Scheme for DebugScheme { /// Write the `buffer` to the `file` /// /// Returns the number of bytes written - fn write(&mut self, _file: usize, buffer: &[u8]) -> Result { + fn write(&self, _file: usize, buffer: &[u8]) -> Result { //TODO: Write bytes, do not convert to str print!("{}", unsafe { str::from_utf8_unchecked(buffer) }); Ok(buffer.len()) } - fn fsync(&mut self, _file: usize) -> Result<()> { + fn fsync(&self, _file: usize) -> Result<()> { Ok(()) } /// Close the file `number` - fn close(&mut self, _file: usize) -> Result<()> { + fn close(&self, _file: usize) -> Result<()> { Ok(()) } } diff --git a/kernel/scheme/env.rs b/kernel/scheme/env.rs index 6e57783..98c0948 100644 --- a/kernel/scheme/env.rs +++ b/kernel/scheme/env.rs @@ -1,4 +1,6 @@ use collections::BTreeMap; +use core::sync::atomic::{AtomicUsize, Ordering}; +use spin::RwLock; use syscall::{Error, Result}; use super::Scheme; @@ -9,9 +11,9 @@ struct Handle { } pub struct EnvScheme { - next_id: usize, + next_id: AtomicUsize, files: BTreeMap<&'static [u8], &'static [u8]>, - handles: BTreeMap + handles: RwLock> } impl EnvScheme { @@ -24,20 +26,19 @@ impl EnvScheme { files.insert(b"LINES", b"30"); EnvScheme { - next_id: 0, + next_id: AtomicUsize::new(0), files: files, - handles: BTreeMap::new() + handles: RwLock::new(BTreeMap::new()) } } } impl Scheme for EnvScheme { - fn open(&mut self, path: &[u8], _flags: usize) -> Result { + fn open(&self, path: &[u8], _flags: usize) -> Result { let data = self.files.get(path).ok_or(Error::NoEntry)?; - let id = self.next_id; - self.next_id += 1; - self.handles.insert(id, Handle { + let id = self.next_id.fetch_add(1, Ordering::SeqCst); + self.handles.write().insert(id, Handle { data: data, seek: 0 }); @@ -45,15 +46,15 @@ impl Scheme for EnvScheme { Ok(id) } - fn dup(&mut self, file: usize) -> Result { + fn dup(&self, file: usize) -> Result { let (data, seek) = { - let handle = self.handles.get(&file).ok_or(Error::BadFile)?; + let handles = self.handles.read(); + let handle = handles.get(&file).ok_or(Error::BadFile)?; (handle.data, handle.seek) }; - let id = self.next_id; - self.next_id += 1; - self.handles.insert(id, Handle { + let id = self.next_id.fetch_add(1, Ordering::SeqCst); + self.handles.write().insert(id, Handle { data: data, seek: seek }); @@ -61,8 +62,9 @@ impl Scheme for EnvScheme { Ok(id) } - fn read(&mut self, file: usize, buffer: &mut [u8]) -> Result { - let mut handle = self.handles.get_mut(&file).ok_or(Error::BadFile)?; + fn read(&self, file: usize, buffer: &mut [u8]) -> Result { + let mut handles = self.handles.write(); + let mut handle = handles.get_mut(&file).ok_or(Error::BadFile)?; let mut i = 0; while i < buffer.len() && handle.seek < handle.data.len() { @@ -74,15 +76,15 @@ impl Scheme for EnvScheme { Ok(i) } - fn write(&mut self, _file: usize, _buffer: &[u8]) -> Result { + fn write(&self, _file: usize, _buffer: &[u8]) -> Result { Err(Error::NotPermitted) } - fn fsync(&mut self, _file: usize) -> Result<()> { + fn fsync(&self, _file: usize) -> Result<()> { Ok(()) } - fn close(&mut self, file: usize) -> Result<()> { - self.handles.remove(&file).ok_or(Error::BadFile).and(Ok(())) + fn close(&self, file: usize) -> Result<()> { + self.handles.write().remove(&file).ok_or(Error::BadFile).and(Ok(())) } } diff --git a/kernel/scheme/initfs.rs b/kernel/scheme/initfs.rs index 96d7d35..186d89d 100644 --- a/kernel/scheme/initfs.rs +++ b/kernel/scheme/initfs.rs @@ -1,4 +1,6 @@ use collections::BTreeMap; +use core::sync::atomic::{AtomicUsize, Ordering}; +use spin::RwLock; use syscall::{Error, Result}; use super::Scheme; @@ -9,9 +11,9 @@ struct Handle { } pub struct InitFsScheme { - next_id: usize, + next_id: AtomicUsize, files: BTreeMap<&'static [u8], &'static [u8]>, - handles: BTreeMap + handles: RwLock> } impl InitFsScheme { @@ -22,23 +24,22 @@ impl InitFsScheme { files.insert(b"bin/ion", include_bytes!("../../build/userspace/ion")); files.insert(b"bin/pcid", include_bytes!("../../build/userspace/pcid")); files.insert(b"bin/ps2d", include_bytes!("../../build/userspace/ps2d")); - files.insert(b"etc/init.rc", b"#initfs:bin/pcid\ninitfs:bin/ps2d\n#initfs:bin/ion"); + files.insert(b"etc/init.rc", b"initfs:bin/pcid\ninitfs:bin/ps2d\ninitfs:bin/ion"); InitFsScheme { - next_id: 0, + next_id: AtomicUsize::new(0), files: files, - handles: BTreeMap::new() + handles: RwLock::new(BTreeMap::new()) } } } impl Scheme for InitFsScheme { - fn open(&mut self, path: &[u8], _flags: usize) -> Result { + fn open(&self, path: &[u8], _flags: usize) -> Result { let data = self.files.get(path).ok_or(Error::NoEntry)?; - let id = self.next_id; - self.next_id += 1; - self.handles.insert(id, Handle { + let id = self.next_id.fetch_add(1, Ordering::SeqCst); + self.handles.write().insert(id, Handle { data: data, seek: 0 }); @@ -46,15 +47,15 @@ impl Scheme for InitFsScheme { Ok(id) } - fn dup(&mut self, file: usize) -> Result { + fn dup(&self, file: usize) -> Result { let (data, seek) = { - let handle = self.handles.get(&file).ok_or(Error::BadFile)?; + let handles = self.handles.read(); + let handle = handles.get(&file).ok_or(Error::BadFile)?; (handle.data, handle.seek) }; - let id = self.next_id; - self.next_id += 1; - self.handles.insert(id, Handle { + let id = self.next_id.fetch_add(1, Ordering::SeqCst); + self.handles.write().insert(id, Handle { data: data, seek: seek }); @@ -62,8 +63,9 @@ impl Scheme for InitFsScheme { Ok(id) } - fn read(&mut self, file: usize, buffer: &mut [u8]) -> Result { - let mut handle = self.handles.get_mut(&file).ok_or(Error::BadFile)?; + fn read(&self, file: usize, buffer: &mut [u8]) -> Result { + let mut handles = self.handles.write(); + let mut handle = handles.get_mut(&file).ok_or(Error::BadFile)?; let mut i = 0; while i < buffer.len() && handle.seek < handle.data.len() { @@ -75,15 +77,15 @@ impl Scheme for InitFsScheme { Ok(i) } - fn write(&mut self, _file: usize, _buffer: &[u8]) -> Result { + fn write(&self, _file: usize, _buffer: &[u8]) -> Result { Err(Error::NotPermitted) } - fn fsync(&mut self, _file: usize) -> Result<()> { + fn fsync(&self, _file: usize) -> Result<()> { Ok(()) } - fn close(&mut self, file: usize) -> Result<()> { - self.handles.remove(&file).ok_or(Error::BadFile).and(Ok(())) + fn close(&self, file: usize) -> Result<()> { + self.handles.write().remove(&file).ok_or(Error::BadFile).and(Ok(())) } } diff --git a/kernel/scheme/irq.rs b/kernel/scheme/irq.rs index 2e762d4..41928b0 100644 --- a/kernel/scheme/irq.rs +++ b/kernel/scheme/irq.rs @@ -7,7 +7,7 @@ use super::Scheme; pub struct IrqScheme; impl Scheme for IrqScheme { - fn open(&mut self, path: &[u8], _flags: usize) -> Result { + fn open(&self, path: &[u8], _flags: usize) -> Result { let path_str = str::from_utf8(path).or(Err(Error::NoEntry))?; let id = path_str.parse::().or(Err(Error::NoEntry))?; @@ -19,11 +19,11 @@ impl Scheme for IrqScheme { } } - fn dup(&mut self, file: usize) -> Result { + fn dup(&self, file: usize) -> Result { Ok(file) } - fn read(&mut self, file: usize, buffer: &mut [u8]) -> Result { + fn read(&self, file: usize, buffer: &mut [u8]) -> Result { // Ensures that the length of the buffer is larger than the size of a usize if buffer.len() >= mem::size_of::() { let ack = ACKS.lock()[file]; @@ -41,7 +41,7 @@ impl Scheme for IrqScheme { } } - fn write(&mut self, file: usize, buffer: &[u8]) -> Result { + fn write(&self, file: usize, buffer: &[u8]) -> Result { if buffer.len() >= mem::size_of::() { assert!(buffer.len() >= mem::size_of::()); let ack = unsafe { *(buffer.as_ptr() as *const usize) }; @@ -58,11 +58,11 @@ impl Scheme for IrqScheme { } } - fn fsync(&mut self, _file: usize) -> Result<()> { + fn fsync(&self, _file: usize) -> Result<()> { Ok(()) } - fn close(&mut self, _file: usize) -> Result<()> { + fn close(&self, _file: usize) -> Result<()> { Ok(()) } } diff --git a/kernel/scheme/mod.rs b/kernel/scheme/mod.rs index 6ca5c13..41e7978 100644 --- a/kernel/scheme/mod.rs +++ b/kernel/scheme/mod.rs @@ -11,7 +11,7 @@ use alloc::boxed::Box; use collections::BTreeMap; -use spin::{Once, Mutex, RwLock, RwLockReadGuard, RwLockWriteGuard}; +use spin::{Once, RwLock, RwLockReadGuard, RwLockWriteGuard}; use syscall::{Error, Result}; @@ -32,12 +32,15 @@ pub mod initfs; /// IRQ handling pub mod irq; +/// Userspace schemes +//pub mod user; + /// Limit on number of schemes pub const SCHEME_MAX_SCHEMES: usize = 65536; /// Scheme list type pub struct SchemeList { - map: BTreeMap>>>, + map: BTreeMap>>, names: BTreeMap, usize>, next_id: usize } @@ -53,11 +56,11 @@ impl SchemeList { } /// Get the nth scheme. - pub fn get(&self, id: usize) -> Option<&Arc>>> { + pub fn get(&self, id: usize) -> Option<&Arc>> { self.map.get(&id) } - pub fn get_name(&self, name: &[u8]) -> Option<(usize, &Arc>>)> { + pub fn get_name(&self, name: &[u8]) -> Option<(usize, &Arc>)> { if let Some(&id) = self.names.get(name) { self.get(id).map(|scheme| (id, scheme)) } else { @@ -66,7 +69,7 @@ impl SchemeList { } /// Create a new scheme. - pub fn insert(&mut self, name: Box<[u8]>, scheme: Arc>>) -> Result<&Arc>>> { + pub fn insert(&mut self, name: Box<[u8]>, scheme: Arc>) -> Result<&Arc>> { if self.names.contains_key(&name) { return Err(Error::FileExists); } @@ -99,10 +102,10 @@ static SCHEMES: Once> = Once::new(); /// Initialize schemes, called if needed fn init_schemes() -> RwLock { let mut list: SchemeList = SchemeList::new(); - list.insert(Box::new(*b"debug"), Arc::new(Mutex::new(Box::new(DebugScheme)))).expect("failed to insert debug: scheme"); - list.insert(Box::new(*b"env"), Arc::new(Mutex::new(Box::new(EnvScheme::new())))).expect("failed to insert env: scheme"); - list.insert(Box::new(*b"initfs"), Arc::new(Mutex::new(Box::new(InitFsScheme::new())))).expect("failed to insert initfs: scheme"); - list.insert(Box::new(*b"irq"), Arc::new(Mutex::new(Box::new(IrqScheme)))).expect("failed to insert irq: scheme"); + list.insert(Box::new(*b"debug"), Arc::new(Box::new(DebugScheme))).expect("failed to insert debug: scheme"); + list.insert(Box::new(*b"env"), Arc::new(Box::new(EnvScheme::new()))).expect("failed to insert env: scheme"); + list.insert(Box::new(*b"initfs"), Arc::new(Box::new(InitFsScheme::new()))).expect("failed to insert initfs: scheme"); + list.insert(Box::new(*b"irq"), Arc::new(Box::new(IrqScheme))).expect("failed to insert irq: scheme"); RwLock::new(list) } @@ -121,26 +124,26 @@ pub trait Scheme { /// Open the file at `path` with `flags`. /// /// Returns a file descriptor or an error - fn open(&mut self, path: &[u8], flags: usize) -> Result; + fn open(&self, path: &[u8], flags: usize) -> Result; /// Duplicate an open file descriptor /// /// Returns a file descriptor or an error - fn dup(&mut self, file: usize) -> Result; + fn dup(&self, file: usize) -> Result; /// Read from some file descriptor into the `buffer` /// /// Returns the number of bytes read - fn read(&mut self, file: usize, buffer: &mut [u8]) -> Result; + fn read(&self, file: usize, buffer: &mut [u8]) -> Result; /// Write the `buffer` to the file descriptor /// /// Returns the number of bytes written - fn write(&mut self, file: usize, buffer: &[u8]) -> Result; + fn write(&self, file: usize, buffer: &[u8]) -> Result; /// Sync the file descriptor - fn fsync(&mut self, file: usize) -> Result<()>; + fn fsync(&self, file: usize) -> Result<()>; /// Close the file descriptor - fn close(&mut self, file: usize) -> Result<()>; + fn close(&self, file: usize) -> Result<()>; } diff --git a/kernel/scheme/user.rs b/kernel/scheme/user.rs new file mode 100644 index 0000000..23b9c71 --- /dev/null +++ b/kernel/scheme/user.rs @@ -0,0 +1,217 @@ +use alloc::arc::{Arc, Weak}; +use alloc::boxed::Box; + +use collections::{BTreeMap, VecDeque}; + +use core::cell::Cell; +use core::mem::size_of; +use core::ops::DerefMut; +use core::{ptr, slice}; + +use syscall::{Call, Error, Result}; + +use super::Scheme; + +/// UserScheme has to be wrapped +pub struct UserScheme { + next_id: Cell, + todo: VecDeque<(usize, (usize, usize, usize, usize))>, + done: BTreeMap, +} + +impl UserScheme { + fn call(&self, a: Call, b: usize, c: usize, d: usize) -> Result { + let id = self.next_id.get(); + + //TODO: What should be done about collisions in self.todo or self.done? + let mut next_id = id + 1; + if next_id <= 0 { + next_id = 1; + } + self.next_id.set(next_id); + + // println!("{} {}: {} {} {:X} {:X} {:X}", UserScheme.name, id, a, ::syscall::name(a), b, c, d); + + Ok(0) + } + + fn capture(&self, mut physical_address: usize, size: usize, writeable: bool) -> Result { + Ok(0) + } + + fn release(&self, virtual_address: usize) { + + } +} + +impl Scheme for UserScheme { + fn open(&mut self, path: &[u8], flags: usize) -> Result { + let virtual_address = try!(self.capture(path.as_ptr() as usize, path.len(), false)); + + let result = self.call(Call::Open, virtual_address, path.len(), flags); + + self.release(virtual_address); + + result + } + + /* + fn mkdir(&mut self, path: &str, flags: usize) -> Result<()> { + let virtual_address = try!(self.capture(path.as_ptr() as usize, path.len(), false)); + + let result = self.call(Call::MkDir, virtual_address, path.len(), flags); + + self.release(virtual_address); + + result.and(Ok(())) + } + + fn rmdir(&mut self, path: &str) -> Result<()> { + let virtual_address = try!(self.capture(path.as_ptr() as usize, path.len(), false)); + + let result = self.call(SYS_RMDIR, virtual_address, path.len(), 0); + + self.release(virtual_address); + + result.and(Ok(())) + } + + fn unlink(&mut self, path: &str) -> Result<()> { + let virtual_address = try!(self.capture(path.as_ptr() as usize, path.len(), false)); + + let result = self.call(SYS_UNLINK, virtual_address, path.len(), 0); + + self.release(virtual_address); + + result.and(Ok(())) + } + */ + + /// Duplicate the resource + fn dup(&mut self, file: usize) -> Result { + self.call(Call::Dup, file, 0, 0) + } + + /* + /// Return the URL of this resource + fn path(&self, file: usize, buf: &mut [u8]) -> Result { + let contexts = unsafe { & *::env().contexts.get() }; + let current = try!(contexts.current()); + if let Ok(physical_address) = current.translate(buf.as_mut_ptr() as usize, buf.len()) { + let offset = physical_address % 4096; + + let virtual_address = try!(self.capture(physical_address - offset, buf.len() + offset, true)); + + let result = self.call(SYS_FPATH, file, virtual_address + offset, buf.len()); + + //println!("Read {:X} mapped from {:X} to {:X} offset {} length {} size {} result {:?}", physical_address, buf.as_ptr() as usize, virtual_address + offset, offset, buf.len(), virtual_size, result); + + self.release(virtual_address); + + result + } else { + println!("{}:{} fault {:X} {}", file!(), line!(), buf.as_ptr() as usize, buf.len()); + Err(Error::Fault) + } + } + */ + + /// Read data to buffer + fn read(&mut self, file: usize, buf: &mut [u8]) -> Result { + /* + let contexts = unsafe { & *::env().contexts.get() }; + let current = try!(contexts.current()); + if let Ok(physical_address) = current.translate(buf.as_mut_ptr() as usize, buf.len()) { + let offset = physical_address % 4096; + + let virtual_address = try!(self.capture(physical_address - offset, buf.len() + offset, true)); + + let result = self.call(Call::Read, file, virtual_address + offset, buf.len()); + + //println!("Read {:X} mapped from {:X} to {:X} offset {} length {} size {} result {:?}", physical_address, buf.as_ptr() as usize, virtual_address + offset, offset, buf.len(), virtual_size, result); + + self.release(virtual_address); + + result + } else */ + { + println!("{}:{} fault {:X} {}", file!(), line!(), buf.as_ptr() as usize, buf.len()); + Err(Error::Fault) + } + } + + /// Write to resource + fn write(&mut self, file: usize, buf: &[u8]) -> Result { + /* + let contexts = unsafe { & *::env().contexts.get() }; + let current = try!(contexts.current()); + if let Ok(physical_address) = current.translate(buf.as_ptr() as usize, buf.len()) { + let offset = physical_address % 4096; + + let virtual_address = try!(self.capture(physical_address - offset, buf.len() + offset, false)); + + let result = self.call(Call::Write, file, virtual_address + offset, buf.len()); + + // println!("Write {:X} mapped from {:X} to {:X} offset {} length {} result {:?}", physical_address, buf.as_ptr() as usize, virtual_address + offset, offset, buf.len(), result); + + self.release(virtual_address); + + result + } else */ + { + println!("{}:{} fault {:X} {}", file!(), line!(), buf.as_ptr() as usize, buf.len()); + Err(Error::Fault) + } + } + + /* + /// Seek + fn seek(&mut self, file: usize, pos: ResourceSeek) -> Result { + let (whence, offset) = match pos { + ResourceSeek::Start(offset) => (SEEK_SET, offset as usize), + ResourceSeek::Current(offset) => (SEEK_CUR, offset as usize), + ResourceSeek::End(offset) => (SEEK_END, offset as usize) + }; + + self.call(SYS_LSEEK, file, offset, whence) + } + + /// Stat the resource + fn stat(&self, file: usize, stat: &mut Stat) -> Result<()> { + let buf = unsafe { slice::from_raw_parts_mut(stat as *mut Stat as *mut u8, size_of::()) }; + + let contexts = unsafe { & *::env().contexts.get() }; + let current = try!(contexts.current()); + if let Ok(physical_address) = current.translate(buf.as_mut_ptr() as usize, buf.len()) { + let offset = physical_address % 4096; + + let virtual_address = try!(self.capture(physical_address - offset, buf.len() + offset, true)); + + let result = self.call(SYS_FSTAT, file, virtual_address + offset, 0); + + self.release(virtual_address); + + result.and(Ok(())) + } else { + println!("{}:{} fault {:X} {}", file!(), line!(), buf.as_ptr() as usize, buf.len()); + Err(Error::Fault) + } + } + */ + + /// Sync the resource + fn fsync(&mut self, file: usize) -> Result<()> { + self.call(Call::FSync, file, 0, 0).and(Ok(())) + } + + /* + /// Truncate the resource + fn truncate(&mut self, file: usize, len: usize) -> Result<()> { + self.call(SYS_FTRUNCATE, file, len, 0).and(Ok(())) + } + */ + + fn close(&mut self, file: usize) -> Result<()> { + self.call(Call::Close, file, 0, 0).and(Ok(())) + } +} diff --git a/kernel/syscall/error.rs b/kernel/syscall/error.rs index ad37b33..e71a8ca 100644 --- a/kernel/syscall/error.rs +++ b/kernel/syscall/error.rs @@ -15,12 +15,18 @@ pub enum Error { BadFile = 9, /// Try again TryAgain = 11, + /// Bad address + Fault = 14, /// File exists FileExists = 17, + /// No such device + NoDevice = 19, /// Invalid argument InvalidValue = 22, /// Too many open files TooManyFiles = 24, + /// Illegal seek + IllegalSeek = 29, /// Syscall not implemented NoCall = 38 } diff --git a/kernel/syscall/fs.rs b/kernel/syscall/fs.rs index d68727d..15a2453 100644 --- a/kernel/syscall/fs.rs +++ b/kernel/syscall/fs.rs @@ -5,6 +5,7 @@ use scheme; use super::{Error, Result}; +/// Change the current working directory pub fn chdir(path: &[u8]) -> Result { let contexts = context::contexts(); let context_lock = contexts.current().ok_or(Error::NoProcess)?; @@ -14,6 +15,7 @@ pub fn chdir(path: &[u8]) -> Result { Ok(0) } +/// Get the current working directory pub fn getcwd(buf: &mut [u8]) -> Result { let contexts = context::contexts(); let context_lock = contexts.current().ok_or(Error::NoProcess)?; @@ -27,38 +29,6 @@ pub fn getcwd(buf: &mut [u8]) -> Result { Ok(i) } -/// Read syscall -pub fn read(fd: usize, buf: &mut [u8]) -> Result { - let file = { - let contexts = context::contexts(); - let context_lock = contexts.current().ok_or(Error::NoProcess)?; - let context = context_lock.read(); - let file = context.get_file(fd).ok_or(Error::BadFile)?; - file - }; - - let schemes = scheme::schemes(); - let scheme_mutex = schemes.get(file.scheme).ok_or(Error::BadFile)?; - let result = scheme_mutex.lock().read(file.number, buf); - result -} - -/// Write syscall -pub fn write(fd: usize, buf: &[u8]) -> Result { - let file = { - let contexts = context::contexts(); - let context_lock = contexts.current().ok_or(Error::NoProcess)?; - let context = context_lock.read(); - let file = context.get_file(fd).ok_or(Error::BadFile)?; - file - }; - - let schemes = scheme::schemes(); - let scheme_mutex = schemes.get(file.scheme).ok_or(Error::BadFile)?; - let result = scheme_mutex.lock().write(file.number, buf); - result -} - /// Open syscall pub fn open(path: &[u8], flags: usize) -> Result { let path_canon = { @@ -74,9 +44,12 @@ pub fn open(path: &[u8], flags: usize) -> Result { let (scheme_id, file_id) = { let namespace = namespace_opt.ok_or(Error::NoEntry)?; - let schemes = scheme::schemes(); - let (scheme_id, scheme_mutex) = schemes.get_name(namespace).ok_or(Error::NoEntry)?; - let file_id = scheme_mutex.lock().open(reference_opt.unwrap_or(b""), flags)?; + let (scheme_id, scheme) = { + let schemes = scheme::schemes(); + let (scheme_id, scheme) = schemes.get_name(namespace).ok_or(Error::NoEntry)?; + (scheme_id, scheme.clone()) + }; + let file_id = scheme.open(reference_opt.unwrap_or(b""), flags)?; (scheme_id, file_id) }; @@ -99,9 +72,12 @@ pub fn close(fd: usize) -> Result { file }; - let schemes = scheme::schemes(); - let scheme_mutex = schemes.get(file.scheme).ok_or(Error::BadFile)?; - let result = scheme_mutex.lock().close(file.number).and(Ok(0)); + let scheme = { + let schemes = scheme::schemes(); + let scheme = schemes.get(file.scheme).ok_or(Error::BadFile)?; + scheme.clone() + }; + let result = scheme.close(file.number).and(Ok(0)); result } @@ -115,12 +91,16 @@ pub fn dup(fd: usize) -> Result { file }; - let schemes = scheme::schemes(); - let scheme_mutex = schemes.get(file.scheme).ok_or(Error::BadFile)?; - let result = scheme_mutex.lock().dup(file.number); + let scheme = { + let schemes = scheme::schemes(); + let scheme = schemes.get(file.scheme).ok_or(Error::BadFile)?; + scheme.clone() + }; + let result = scheme.dup(file.number); result } +/// Sync the file descriptor pub fn fsync(fd: usize) -> Result { let file = { let contexts = context::contexts(); @@ -130,8 +110,49 @@ pub fn fsync(fd: usize) -> Result { file }; - let schemes = scheme::schemes(); - let scheme_mutex = schemes.get(file.scheme).ok_or(Error::BadFile)?; - let result = scheme_mutex.lock().fsync(file.number).and(Ok(0)); + let scheme = { + let schemes = scheme::schemes(); + let scheme = schemes.get(file.scheme).ok_or(Error::BadFile)?; + scheme.clone() + }; + let result = scheme.fsync(file.number).and(Ok(0)); + result +} + +/// Read syscall +pub fn read(fd: usize, buf: &mut [u8]) -> Result { + let file = { + let contexts = context::contexts(); + let context_lock = contexts.current().ok_or(Error::NoProcess)?; + let context = context_lock.read(); + let file = context.get_file(fd).ok_or(Error::BadFile)?; + file + }; + + let scheme = { + let schemes = scheme::schemes(); + let scheme = schemes.get(file.scheme).ok_or(Error::BadFile)?; + scheme.clone() + }; + let result = scheme.read(file.number, buf); + result +} + +/// Write syscall +pub fn write(fd: usize, buf: &[u8]) -> Result { + let file = { + let contexts = context::contexts(); + let context_lock = contexts.current().ok_or(Error::NoProcess)?; + let context = context_lock.read(); + let file = context.get_file(fd).ok_or(Error::BadFile)?; + file + }; + + let scheme = { + let schemes = scheme::schemes(); + let scheme = schemes.get(file.scheme).ok_or(Error::BadFile)?; + scheme.clone() + }; + let result = scheme.write(file.number, buf); result } diff --git a/kernel/syscall/process.rs b/kernel/syscall/process.rs index f05271a..081b587 100644 --- a/kernel/syscall/process.rs +++ b/kernel/syscall/process.rs @@ -178,9 +178,12 @@ pub fn clone(flags: usize, stack_base: usize) -> Result { for (fd, file_option) in context.files.lock().iter().enumerate() { if let Some(file) = *file_option { let result = { - let schemes = scheme::schemes(); - let scheme_mutex = schemes.get(file.scheme).ok_or(Error::BadFile)?; - let result = scheme_mutex.lock().dup(file.number); + let scheme = { + let schemes = scheme::schemes(); + let scheme = schemes.get(file.scheme).ok_or(Error::BadFile)?; + scheme.clone() + }; + let result = scheme.dup(file.number); result }; match result {