Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 18 additions & 19 deletions arceos/api/arceos_posix_api/src/imp/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use core::ffi::{c_char, c_int};

use axerrno::{LinuxError, LinuxResult};
use axfs::OpenOptions;
use axfs_ng_vfs::{NodePermission, VfsError};
use axio::PollState;
use axsync::Mutex;

Expand All @@ -16,10 +17,7 @@ pub struct File {

impl File {
fn new(inner: axfs::File) -> Self {
Self {
inner: Mutex::new(inner),
offset: Mutex::new(0),
}
Self { inner: Mutex::new(inner), offset: Mutex::new(0) }
}

fn add_to_fd_table(self) -> LinuxResult<c_int> {
Expand All @@ -28,9 +26,7 @@ impl File {

fn from_fd(fd: c_int) -> LinuxResult<Arc<Self>> {
let f = super::fd_ops::get_file_like(fd)?;
f.into_any()
.downcast::<Self>()
.map_err(|_| LinuxError::EINVAL)
f.into_any().downcast::<Self>().map_err(|_| LinuxError::EINVAL)
}
}

Expand Down Expand Up @@ -81,10 +77,7 @@ impl FileLike for File {
}

fn poll(&self) -> LinuxResult<PollState> {
Ok(PollState {
readable: true,
writable: true,
})
Ok(PollState { readable: true, writable: true })
}

fn set_nonblocking(&self, _nonblocking: bool) -> LinuxResult {
Expand All @@ -99,16 +92,11 @@ pub struct DirFile {

impl DirFile {
fn new(dir: axfs::OpenResult) -> Self {
Self {
inner: Mutex::new(dir),
offset: Mutex::new(0),
}
Self { inner: Mutex::new(dir), offset: Mutex::new(0) }
}
fn from_fd(fd: c_int) -> LinuxResult<Arc<Self>> {
let f = super::fd_ops::get_file_like(fd)?;
f.into_any()
.downcast::<Self>()
.map_err(|_| LinuxError::EBADF)
f.into_any().downcast::<Self>().map_err(|_| LinuxError::EBADF)
}
}

Expand Down Expand Up @@ -393,7 +381,18 @@ pub fn sys_mkdir(path: *const c_char, _mode: ctypes::mode_t) -> c_int {
debug!("sys_mkdir <= {:?}", path);
syscall_body!(sys_mkdir, {
let path = path?;
axfs::FS_CONTEXT.lock().create_dir(path, Default::default())?;
let fs = axfs::FS_CONTEXT.lock();
if fs.resolve(path).is_ok() {
return Err(LinuxError::EEXIST);
}
let umask =
pulse_core::task::current_process().map(|process| process.umask()).unwrap_or(0o022);
let mode = (((_mode as u32) & !umask) & 0o777) as _;
match fs.create_dir(path, NodePermission::from_bits_truncate(mode)) {
Ok(_) => {}
Err(VfsError::NotFound) => return Err(LinuxError::ENOENT),
Err(err) => return Err(LinuxError::from(err.canonicalize())),
}
Ok(0)
})
}
Expand Down
93 changes: 78 additions & 15 deletions arceos/modules/axfs/src/disk.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
use alloc::{boxed::Box, vec};
use alloc::{
boxed::Box,
string::{String, ToString},
sync::Arc,
vec,
};
use core::mem;

use axdriver::prelude::*;
use axdriver::{AxBlockDevice, prelude::*};
use spin::Mutex;

fn take<'a>(buf: &mut &'a [u8], cnt: usize) -> &'a [u8] {
let (first, rem) = buf.split_at(cnt);
Expand All @@ -16,9 +22,73 @@ fn take_mut<'a>(buf: &mut &'a mut [u8], cnt: usize) -> &'a mut [u8] {
first
}

/// A block device wrapper that can be cloned and shared across subsystems.
#[derive(Clone)]
pub struct SharedBlockDevice {
name: String,
dev: Arc<Mutex<AxBlockDevice>>,
}

impl SharedBlockDevice {
/// Wraps a block device so the same underlying driver can be reused.
pub fn new(dev: AxBlockDevice) -> Self {
let name = dev.device_name().to_string();
Self { name, dev: Arc::new(Mutex::new(dev)) }
}

/// Returns the total size of the device in bytes.
pub fn size(&self) -> u64 {
let dev = self.dev.lock();
dev.num_blocks().saturating_mul(dev.block_size() as u64)
}

/// Returns the device block size.
pub fn block_size(&self) -> usize {
let dev = self.dev.lock();
dev.block_size()
}
}

impl BaseDriverOps for SharedBlockDevice {
fn device_name(&self) -> &str {
&self.name
}

fn device_type(&self) -> DeviceType {
DeviceType::Block
}
}

impl BlockDriverOps for SharedBlockDevice {
fn num_blocks(&self) -> u64 {
let dev = self.dev.lock();
dev.num_blocks()
}

fn block_size(&self) -> usize {
let dev = self.dev.lock();
dev.block_size()
}

fn read_block(&mut self, block_id: u64, buf: &mut [u8]) -> DevResult {
let mut dev = self.dev.lock();
dev.read_block(block_id, buf)
}

fn write_block(&mut self, block_id: u64, buf: &[u8]) -> DevResult {
let mut dev = self.dev.lock();
dev.write_block(block_id, buf)
}

fn flush(&mut self) -> DevResult {
let mut dev = self.dev.lock();
dev.flush()
}
}

/// A disk device with a cursor.
pub struct SeekableDisk {
dev: AxBlockDevice,
pub struct SeekableDisk<D: BlockDriverOps> {
dev: D,

block_id: u64,
offset: usize,
Expand All @@ -32,9 +102,9 @@ pub struct SeekableDisk {
write_buffer_dirty: bool,
}

impl SeekableDisk {
impl<D: BlockDriverOps> SeekableDisk<D> {
/// Create a new disk.
pub fn new(dev: AxBlockDevice) -> Self {
pub fn new(dev: D) -> Self {
assert!(dev.block_size().is_power_of_two());
let block_size_log2 = dev.block_size().trailing_zeros() as u8;
let read_buffer = vec![0u8; dev.block_size()].into_boxed_slice();
Expand All @@ -60,11 +130,6 @@ impl SeekableDisk {
1 << self.block_size_log2
}

/// Get the position of the cursor.
pub fn position(&self) -> u64 {
(self.block_id << self.block_size_log2) + self.offset as u64
}

/// Set the position of the cursor.
pub fn set_position(&mut self, pos: u64) -> DevResult<()> {
self.flush()?;
Expand Down Expand Up @@ -108,8 +173,7 @@ impl SeekableDisk {
if buf.len() >= self.block_size() {
let blocks = buf.len() >> self.block_size_log2;
let length = blocks << self.block_size_log2;
self.dev
.read_block(self.block_id, take_mut(&mut buf, length))?;
self.dev.read_block(self.block_id, take_mut(&mut buf, length))?;
read += length;

self.block_id += blocks as u64;
Expand Down Expand Up @@ -150,8 +214,7 @@ impl SeekableDisk {
if buf.len() >= self.block_size() {
let blocks = buf.len() >> self.block_size_log2;
let length = blocks << self.block_size_log2;
self.dev
.write_block(self.block_id, take(&mut buf, length))?;
self.dev.write_block(self.block_id, take(&mut buf, length))?;
written += length;

self.block_id += blocks as u64;
Expand Down
Loading