Skip to content
Open
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
4 changes: 2 additions & 2 deletions src/fd/eventfd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ impl EventFd {

#[async_trait]
impl ObjectInterface for EventFd {
async fn read(&self, buf: &mut [u8]) -> io::Result<usize> {
async fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
let len = mem::size_of::<u64>();

if buf.len() < len {
Expand Down Expand Up @@ -90,7 +90,7 @@ impl ObjectInterface for EventFd {
.await
}

async fn write(&self, buf: &[u8]) -> io::Result<usize> {
async fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
let len = mem::size_of::<u64>();

if buf.len() < len {
Expand Down
23 changes: 13 additions & 10 deletions src/fd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,18 +205,18 @@ pub(crate) trait ObjectInterface: Sync + Send {

/// `async_read` attempts to read `len` bytes from the object references
/// by the descriptor
async fn read(&self, _buf: &mut [u8]) -> io::Result<usize> {
async fn read(&mut self, _buf: &mut [u8]) -> io::Result<usize> {
Err(Errno::Nosys)
}

/// `async_write` attempts to write `len` bytes to the object references
/// by the descriptor
async fn write(&self, _buf: &[u8]) -> io::Result<usize> {
async fn write(&mut self, _buf: &[u8]) -> io::Result<usize> {
Err(Errno::Nosys)
}

/// `lseek` function repositions the offset of the file descriptor fildes
async fn lseek(&self, _offset: isize, _whence: SeekWhence) -> io::Result<isize> {
async fn lseek(&mut self, _offset: isize, _whence: SeekWhence) -> io::Result<isize> {
Err(Errno::Inval)
}

Expand Down Expand Up @@ -318,12 +318,12 @@ pub(crate) trait ObjectInterface: Sync + Send {
}

/// Truncates the file
async fn truncate(&self, _size: usize) -> io::Result<()> {
async fn truncate(&mut self, _size: usize) -> io::Result<()> {
Err(Errno::Nosys)
}

/// Changes access permissions to the file
async fn chmod(&self, _access_permission: AccessPermission) -> io::Result<()> {
async fn chmod(&mut self, _access_permission: AccessPermission) -> io::Result<()> {
Err(Errno::Nosys)
}

Expand All @@ -340,19 +340,22 @@ pub(crate) fn read(fd: RawFd, buf: &mut [u8]) -> io::Result<usize> {
return Ok(0);
}

block_on(async { obj.read().await.read(buf).await }, None)
block_on(async { obj.write().await.read(buf).await }, None)
}

pub(crate) fn lseek(fd: RawFd, offset: isize, whence: SeekWhence) -> io::Result<isize> {
let obj = get_object(fd)?;

block_on(async { obj.read().await.lseek(offset, whence).await }, None)
block_on(
async { obj.write().await.lseek(offset, whence).await },
None,
)
}

pub(crate) fn chmod(fd: RawFd, mode: AccessPermission) -> io::Result<()> {
let obj = get_object(fd)?;

block_on(async { obj.read().await.chmod(mode).await }, None)
block_on(async { obj.write().await.chmod(mode).await }, None)
}

pub(crate) fn write(fd: RawFd, buf: &[u8]) -> io::Result<usize> {
Expand All @@ -362,12 +365,12 @@ pub(crate) fn write(fd: RawFd, buf: &[u8]) -> io::Result<usize> {
return Ok(0);
}

block_on(async { obj.read().await.write(buf).await }, None)
block_on(async { obj.write().await.write(buf).await }, None)
}

pub(crate) fn truncate(fd: RawFd, length: usize) -> io::Result<()> {
let obj = get_object(fd)?;
block_on(async { obj.read().await.truncate(length).await }, None)
block_on(async { obj.write().await.truncate(length).await }, None)
}

async fn poll_fds(fds: &mut [PollFd]) -> io::Result<u64> {
Expand Down
4 changes: 2 additions & 2 deletions src/fd/socket/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ impl ObjectInterface for Socket {
.await
}

async fn read(&self, buffer: &mut [u8]) -> io::Result<usize> {
async fn read(&mut self, buffer: &mut [u8]) -> io::Result<usize> {
future::poll_fn(|cx| {
self.with(|socket| {
let state = socket.state();
Expand Down Expand Up @@ -227,7 +227,7 @@ impl ObjectInterface for Socket {
.await
}

async fn write(&self, buffer: &[u8]) -> io::Result<usize> {
async fn write(&mut self, buffer: &[u8]) -> io::Result<usize> {
let mut pos: usize = 0;

while pos < buffer.len() {
Expand Down
4 changes: 2 additions & 2 deletions src/fd/socket/udp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ impl ObjectInterface for Socket {
.map(|(len, endpoint)| (len, Endpoint::Ip(endpoint)))
}

async fn read(&self, buffer: &mut [u8]) -> io::Result<usize> {
async fn read(&mut self, buffer: &mut [u8]) -> io::Result<usize> {
future::poll_fn(|cx| {
self.with(|socket| {
if socket.is_open() {
Expand Down Expand Up @@ -220,7 +220,7 @@ impl ObjectInterface for Socket {
.await
}

async fn write(&self, buf: &[u8]) -> io::Result<usize> {
async fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
let endpoint = self.remote_endpoint.ok_or(Errno::Inval)?;

let meta = UdpMetadata::from(endpoint);
Expand Down
4 changes: 2 additions & 2 deletions src/fd/socket/vsock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ impl ObjectInterface for Socket {
Ok(())
}

async fn read(&self, buffer: &mut [u8]) -> io::Result<usize> {
async fn read(&mut self, buffer: &mut [u8]) -> io::Result<usize> {
let port = self.port;
future::poll_fn(|cx| {
let mut guard = VSOCK_MAP.lock();
Expand Down Expand Up @@ -362,7 +362,7 @@ impl ObjectInterface for Socket {
.await
}

async fn write(&self, buffer: &[u8]) -> io::Result<usize> {
async fn write(&mut self, buffer: &[u8]) -> io::Result<usize> {
let port = self.port;
future::poll_fn(|cx| {
let mut guard = VSOCK_MAP.lock();
Expand Down
10 changes: 5 additions & 5 deletions src/fd/stdio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl ObjectInterface for GenericStdin {
Ok(event & available)
}

async fn read(&self, buf: &mut [u8]) -> io::Result<usize> {
async fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
future::poll_fn(|cx| {
let read_bytes = CONSOLE.lock().read(buf)?;
if read_bytes > 0 {
Expand Down Expand Up @@ -71,7 +71,7 @@ impl ObjectInterface for GenericStdout {
Ok(event & available)
}

async fn write(&self, buf: &[u8]) -> io::Result<usize> {
async fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
CONSOLE.lock().write(buf)
}

Expand Down Expand Up @@ -103,7 +103,7 @@ impl ObjectInterface for GenericStderr {
Ok(event & available)
}

async fn write(&self, buf: &[u8]) -> io::Result<usize> {
async fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
CONSOLE.lock().write(buf)
}

Expand Down Expand Up @@ -158,7 +158,7 @@ impl ObjectInterface for UhyveStdout {
Ok(event & available)
}

async fn write(&self, buf: &[u8]) -> io::Result<usize> {
async fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
let write_params = WriteParams {
fd: STDOUT_FILENO,
buf: GuestVirtAddr::from_ptr(buf.as_ptr()),
Expand Down Expand Up @@ -197,7 +197,7 @@ impl ObjectInterface for UhyveStderr {
Ok(event & available)
}

async fn write(&self, buf: &[u8]) -> io::Result<usize> {
async fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
let write_params = WriteParams {
fd: STDERR_FILENO,
buf: GuestVirtAddr::from_ptr(buf.as_ptr()),
Expand Down
16 changes: 8 additions & 8 deletions src/fs/mem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ impl ObjectInterface for RomFileInterface {
Ok(ret)
}

async fn read(&self, buf: &mut [u8]) -> io::Result<usize> {
async fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
{
let microseconds = arch::kernel::systemtime::now_micros();
let t = timespec::from_usec(microseconds as i64);
Expand All @@ -81,7 +81,7 @@ impl ObjectInterface for RomFileInterface {
Ok(len)
}

async fn lseek(&self, offset: isize, whence: SeekWhence) -> io::Result<isize> {
async fn lseek(&mut self, offset: isize, whence: SeekWhence) -> io::Result<isize> {
// NOTE: Allocations can never be larger than `isize::MAX` bytes.
let data_len = self.inner.data.len() as isize;

Expand Down Expand Up @@ -155,7 +155,7 @@ impl ObjectInterface for RamFileInterface {
Ok(event & available)
}

async fn read(&self, buf: &mut [u8]) -> io::Result<usize> {
async fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
{
let microseconds = arch::kernel::systemtime::now_micros();
let t = timespec::from_usec(microseconds as i64);
Expand Down Expand Up @@ -183,7 +183,7 @@ impl ObjectInterface for RamFileInterface {
Ok(len)
}

async fn write(&self, buf: &[u8]) -> io::Result<usize> {
async fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
let microseconds = arch::kernel::systemtime::now_micros();
let t = timespec::from_usec(microseconds as i64);
let mut guard = self.inner.write().await;
Expand All @@ -205,7 +205,7 @@ impl ObjectInterface for RamFileInterface {
Ok(buf.len())
}

async fn lseek(&self, offset: isize, whence: SeekWhence) -> io::Result<isize> {
async fn lseek(&mut self, offset: isize, whence: SeekWhence) -> io::Result<isize> {
let mut guard = self.inner.write().await;
let mut pos_guard = self.pos.lock().await;

Expand Down Expand Up @@ -239,14 +239,14 @@ impl ObjectInterface for RamFileInterface {
Ok(guard.attr)
}

async fn truncate(&self, size: usize) -> io::Result<()> {
async fn truncate(&mut self, size: usize) -> io::Result<()> {
let mut guard = self.inner.write().await;
guard.data.resize(size, 0);
guard.attr.st_size = size as i64;
Ok(())
}

async fn chmod(&self, access_permission: AccessPermission) -> io::Result<()> {
async fn chmod(&mut self, access_permission: AccessPermission) -> io::Result<()> {
let mut guard = self.inner.write().await;
guard.attr.st_mode = access_permission;
Ok(())
Expand Down Expand Up @@ -435,7 +435,7 @@ impl ObjectInterface for MemDirectoryInterface {
/// logically the same operation, so we can just use the same fn in the backend.
/// Any other offset than 0 is not supported. (Mostly because it doesn't make any sense, as
/// userspace applications have no way of knowing valid offsets)
async fn lseek(&self, offset: isize, whence: SeekWhence) -> io::Result<isize> {
async fn lseek(&mut self, offset: isize, whence: SeekWhence) -> io::Result<isize> {
if whence != SeekWhence::Set && offset != 0 {
error!("Invalid offset for directory lseek ({offset})");
return Err(Errno::Inval);
Expand Down
2 changes: 1 addition & 1 deletion src/fs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ pub fn truncate(name: &str, size: usize) -> io::Result<()> {
.open(name, OpenOption::O_TRUNC, AccessPermission::empty())
.map_err(|_| Errno::Badf)?;

block_on(async { file.read().await.truncate(size).await }, None)
block_on(async { file.write().await.truncate(size).await }, None)
})
}

Expand Down
55 changes: 18 additions & 37 deletions src/fs/uhyve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use alloc::string::String;
use alloc::sync::Arc;
use alloc::vec::Vec;

use async_lock::Mutex;
use async_trait::async_trait;
use embedded_io::{ErrorType, Read, Write};
use memory_addresses::VirtAddr;
Expand All @@ -25,14 +24,25 @@ use crate::io;
use crate::syscalls::interfaces::uhyve::uhyve_hypercall;

#[derive(Debug)]
struct UhyveFileHandleInner(i32);
struct UhyveFileHandle(i32);

impl UhyveFileHandleInner {
impl UhyveFileHandle {
pub fn new(fd: i32) -> Self {
Self(fd)
}
}

fn lseek(&self, offset: isize, whence: SeekWhence) -> io::Result<isize> {
#[async_trait]
impl ObjectInterface for UhyveFileHandle {
async fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
Read::read(self, buf)
}

async fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
Write::write(self, buf)
}

async fn lseek(&mut self, offset: isize, whence: SeekWhence) -> io::Result<isize> {
let mut lseek_params = LseekParams {
fd: self.0,
offset,
Expand All @@ -48,11 +58,11 @@ impl UhyveFileHandleInner {
}
}

impl ErrorType for UhyveFileHandleInner {
impl ErrorType for UhyveFileHandle {
type Error = Errno;
}

impl Read for UhyveFileHandleInner {
impl Read for UhyveFileHandle {
fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error> {
let mut read_params = ReadParams {
fd: self.0,
Expand All @@ -70,7 +80,7 @@ impl Read for UhyveFileHandleInner {
}
}

impl Write for UhyveFileHandleInner {
impl Write for UhyveFileHandle {
fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> {
let write_params = WriteParams {
fd: self.0,
Expand All @@ -87,7 +97,7 @@ impl Write for UhyveFileHandleInner {
}
}

impl Drop for UhyveFileHandleInner {
impl Drop for UhyveFileHandle {
fn drop(&mut self) {
let mut close_params = CloseParams { fd: self.0, ret: 0 };
uhyve_hypercall(Hypercall::FileClose(&mut close_params));
Expand All @@ -98,35 +108,6 @@ impl Drop for UhyveFileHandleInner {
}
}

struct UhyveFileHandle(Arc<Mutex<UhyveFileHandleInner>>);

impl UhyveFileHandle {
pub fn new(fd: i32) -> Self {
Self(Arc::new(Mutex::new(UhyveFileHandleInner::new(fd))))
}
}

#[async_trait]
impl ObjectInterface for UhyveFileHandle {
async fn read(&self, buf: &mut [u8]) -> io::Result<usize> {
self.0.lock().await.read(buf)
}

async fn write(&self, buf: &[u8]) -> io::Result<usize> {
self.0.lock().await.write(buf)
}

async fn lseek(&self, offset: isize, whence: SeekWhence) -> io::Result<isize> {
self.0.lock().await.lseek(offset, whence)
}
}

impl Clone for UhyveFileHandle {
fn clone(&self) -> Self {
Self(self.0.clone())
}
}

#[derive(Debug)]
pub(crate) struct UhyveDirectory {
prefix: Option<String>,
Expand Down
Loading
Loading