Mount ramfs at '/dev/shm'

This commit is contained in:
LI Qing 2021-03-31 17:15:28 +08:00 committed by Zongmin.Gu
parent 1cf270634f
commit 8db6a55696
5 changed files with 87 additions and 5 deletions

2
deps/sefs vendored

@ -1 +1 @@
Subproject commit bb574e0c0bb0a3034f1f4eb48b1d559fe84d2555 Subproject commit 6bf6fc267b9712a60477727506ed96a37e5f21b2

@ -0,0 +1,41 @@
use super::*;
#[derive(Debug)]
pub struct DevShm;
impl INode for DevShm {
fn read_at(&self, offset: usize, buf: &mut [u8]) -> vfs::Result<usize> {
Err(vfs::FsError::NotFile)
}
fn write_at(&self, offset: usize, buf: &[u8]) -> vfs::Result<usize> {
Err(vfs::FsError::NotFile)
}
fn poll(&self) -> vfs::Result<vfs::PollStatus> {
Err(vfs::FsError::NotFile)
}
fn metadata(&self) -> vfs::Result<Metadata> {
Ok(Metadata {
dev: 0,
inode: 2,
size: 0,
blk_size: 0,
blocks: 0,
atime: Timespec { sec: 0, nsec: 0 },
mtime: Timespec { sec: 0, nsec: 0 },
ctime: Timespec { sec: 0, nsec: 0 },
type_: vfs::FileType::Dir,
mode: 0o777,
nlinks: 1,
uid: 0,
gid: 0,
rdev: 0,
})
}
fn as_any_ref(&self) -> &dyn Any {
self
}
}

@ -1,19 +1,25 @@
use super::rootfs::mount_fs_at;
use super::*; use super::*;
use rcore_fs::vfs; use rcore_fs::vfs;
use rcore_fs_devfs::DevFS; use rcore_fs_devfs::DevFS;
use rcore_fs_mountfs::MountFS;
use rcore_fs_ramfs::RamFS;
use self::dev_null::DevNull; use self::dev_null::DevNull;
use self::dev_random::DevRandom; use self::dev_random::DevRandom;
use self::dev_sgx::DevSgx; use self::dev_sgx::DevSgx;
use self::dev_shm::DevShm;
use self::dev_zero::DevZero; use self::dev_zero::DevZero;
mod dev_null; mod dev_null;
mod dev_random; mod dev_random;
mod dev_sgx; mod dev_sgx;
mod dev_shm;
mod dev_zero; mod dev_zero;
/// API to initialize the DevFS /// API to initialize the DevFS
pub fn init_devfs() -> Result<Arc<DevFS>> { pub fn init_devfs() -> Result<Arc<MountFS>> {
let devfs = DevFS::new(); let devfs = DevFS::new();
let dev_null = Arc::new(DevNull) as _; let dev_null = Arc::new(DevNull) as _;
devfs.add("null", dev_null)?; devfs.add("null", dev_null)?;
@ -25,6 +31,12 @@ pub fn init_devfs() -> Result<Arc<DevFS>> {
devfs.add("arandom", Arc::clone(&dev_random))?; devfs.add("arandom", Arc::clone(&dev_random))?;
let dev_sgx = Arc::new(DevSgx) as _; let dev_sgx = Arc::new(DevSgx) as _;
devfs.add("sgx", dev_sgx)?; devfs.add("sgx", dev_sgx)?;
let dev_shm = Arc::new(DevShm) as _;
devfs.add("shm", dev_shm)?;
let mountable_devfs = MountFS::new(devfs);
// Mount the ramfs at '/shm'
let ramfs = RamFS::new();
mount_fs_at(ramfs, &mountable_devfs.root_inode(), &Path::new("/shm"))?;
// TODO: Add stdio(stdin, stdout, stderr) into DevFS // TODO: Add stdio(stdin, stdout, stderr) into DevFS
Ok(devfs) Ok(mountable_devfs)
} }

@ -123,7 +123,7 @@ pub fn mount_nonroot_fs_according_to(
Ok(()) Ok(())
} }
fn mount_fs_at(fs: Arc<dyn FileSystem>, parent_inode: &MNode, abs_path: &Path) -> Result<()> { pub fn mount_fs_at(fs: Arc<dyn FileSystem>, parent_inode: &MNode, abs_path: &Path) -> Result<()> {
let mut mount_dir = parent_inode.find(false, ".")?; let mut mount_dir = parent_inode.find(false, ".")?;
// The first component of abs_path is the RootDir, skip it. // The first component of abs_path is the RootDir, skip it.
for dirname in abs_path.iter().skip(1) { for dirname in abs_path.iter().skip(1) {

@ -4,7 +4,7 @@
#include <poll.h> #include <poll.h>
#include <unistd.h> #include <unistd.h>
#include <stdio.h> #include <stdio.h>
#include "test.h" #include "test_fs.h"
// ============================================================================ // ============================================================================
// Test utilities // Test utilities
@ -115,6 +115,34 @@ int test_dev_arandom() {
return 0; return 0;
} }
int test_dev_shm() {
struct stat stat_buf;
if (stat("/dev/shm", &stat_buf) < 0) {
THROW_ERROR("failed to stat /dev/shm");
}
if (!S_ISDIR(stat_buf.st_mode)) {
THROW_ERROR("failed to check if it is dir");
}
char *write_str = "Hello World\n";
char *file_path = "/dev/shm/test_read_write.txt";
int fd = open(file_path, O_WRONLY | O_CREAT | O_TRUNC, 00666);
if (fd < 0) {
THROW_ERROR("failed to open a file to write");
}
if (write(fd, write_str, strlen(write_str)) <= 0) {
THROW_ERROR("failed to write");
}
close(fd);
if (fs_check_file_content(file_path, write_str) < 0) {
THROW_ERROR("failed to check file content");
}
if (unlink(file_path) < 0) {
THROW_ERROR("failed to unlink the file");
}
return 0;
}
// ============================================================================ // ============================================================================
// Test suite // Test suite
// ============================================================================ // ============================================================================
@ -127,6 +155,7 @@ static test_case_t test_cases[] = {
TEST_CASE(test_dev_urandom_fstat), TEST_CASE(test_dev_urandom_fstat),
TEST_CASE(test_dev_urandom_poll), TEST_CASE(test_dev_urandom_poll),
TEST_CASE(test_dev_arandom), TEST_CASE(test_dev_arandom),
TEST_CASE(test_dev_shm),
}; };
int main() { int main() {