Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend the FUSE support #811

Closed
wants to merge 19 commits into from
Closed
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
3 changes: 3 additions & 0 deletions src/errno.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#[thread_local]
pub static mut ERRNO: i32 = 0;

/// Operation not permitted
pub const EPERM: i32 = 1;

Expand Down
32 changes: 31 additions & 1 deletion src/fd/file.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use alloc::boxed::Box;
use core::{isize, slice};

use crate::errno::*;
use crate::fd::{
uhyve_send, ObjectInterface, SysClose, SysLseek, SysRead, SysWrite, UHYVE_PORT_CLOSE,
UHYVE_PORT_LSEEK, UHYVE_PORT_READ, UHYVE_PORT_WRITE,
};
use crate::syscalls::fs::{self, PosixFile, SeekWhence};
use crate::syscalls::fs::{self, Dirent, FileAttr, PosixFile, SeekWhence};

#[derive(Debug, Clone)]
pub struct UhyveFile(i32);
Expand Down Expand Up @@ -98,6 +99,35 @@ impl ObjectInterface for GenericFile {

ret as isize
}

/// `fstat`
fn fstat(&self, stat: *mut FileAttr) -> i32 {
debug!("fstat ! {}", self.0);
let mut result = 0;
let mut fs = fs::FILESYSTEM.lock();
fs.fd_op(self.0, |file: &mut Box<dyn PosixFile + Send>| {
result = file
.fstat(stat)
.map_or_else(|e| -num::ToPrimitive::to_i32(&e).unwrap(), |_| 0);
});

result
}

fn readdir(&self) -> *const Dirent {
debug!("readdir ! {}", self.0);

let mut fs = fs::FILESYSTEM.lock();
let mut ret: *const Dirent = core::ptr::null();
fs.fd_op(self.0, |file: &mut Box<dyn PosixFile + Send>| {
match file.readdir() {
Ok(dir_ptr) => ret = dir_ptr,
Err(e) => unsafe { ERRNO = num::ToPrimitive::to_i32(&e).unwrap() },
}
});

ret
}
}

impl Drop for GenericFile {
Expand Down
59 changes: 57 additions & 2 deletions src/fd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::env;
use crate::errno::*;
use crate::fd::file::{GenericFile, UhyveFile};
use crate::fd::stdio::*;
use crate::syscalls::fs::{self, FilePerms, SeekWhence};
use crate::syscalls::fs::{self, Dirent, FileAttr, FilePerms, SeekWhence};
#[cfg(all(feature = "tcp", not(feature = "newlib")))]
use crate::syscalls::net::*;

Expand Down Expand Up @@ -199,11 +199,36 @@ pub trait ObjectInterface: Sync + Send + core::fmt::Debug + DynClone {
(-EINVAL).try_into().unwrap()
}

/// `unlink` removes directory entry
/// `fstat`
fn fstat(&self, _stat: *mut FileAttr) -> i32 {
-EINVAL
}

/// `unlink` removes file entry
fn unlink(&self, _name: *const u8) -> i32 {
-EINVAL
}

/// `rmdir` removes directory entry
fn rmdir(&self, _name: *const u8) -> i32 {
-EINVAL
}

/// 'readdir' returns a pointer to a dirent structure
/// representing the next directory entry in the directory stream
/// pointed to by the file descriptor
fn readdir(&self) -> *const Dirent {
unsafe {
ERRNO = ENOSYS;
}
core::ptr::null()
}

/// `mkdir` creates a directory entry
fn mkdir(&self, _name: *const u8, _mode: u32) -> i32 {
-EINVAL
}

/// `accept` a connection on a socket
#[cfg(all(feature = "tcp", not(feature = "newlib")))]
fn accept(&self, _addr: *mut sockaddr, _addrlen: *mut socklen_t) -> i32 {
Expand Down Expand Up @@ -319,6 +344,36 @@ pub(crate) fn open(name: *const u8, flags: i32, mode: i32) -> Result<FileDescrip
}
}

pub(crate) fn opendir(name: *const u8) -> Result<FileDescriptor, i32> {
if env::is_uhyve() {
Err(-EINVAL)
} else {
#[cfg(target_arch = "x86_64")]
{
let name = unsafe { CStr::from_ptr(name as _) }.to_str().unwrap();
debug!("Open directory {}", name);

let mut fs = fs::FILESYSTEM.lock();
if let Ok(filesystem_fd) = fs.opendir(name) {
let fd = FD_COUNTER.fetch_add(1, Ordering::SeqCst);
// Would a GenericDir make sense?
let file = GenericFile::new(filesystem_fd);
if OBJECT_MAP.write().try_insert(fd, Arc::new(file)).is_err() {
Err(-EINVAL)
} else {
Ok(fd as FileDescriptor)
}
} else {
Err(-EINVAL)
}
}
#[cfg(not(target_arch = "x86_64"))]
{
Err(-ENOSYS)
}
}
}

pub(crate) fn get_object(fd: FileDescriptor) -> Result<Arc<dyn ObjectInterface>, i32> {
Ok((*(OBJECT_MAP.read().get(&fd).ok_or(-EINVAL)?)).clone())
}
Expand Down
Loading