Skip to content

Commit

Permalink
move mount_*fs out
Browse files Browse the repository at this point in the history
  • Loading branch information
coolyjg committed Jul 27, 2023
1 parent cbcc960 commit 9c4cf10
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 106 deletions.
1 change: 1 addition & 0 deletions modules/axfs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ extern crate alloc;
mod dev;
mod fs;
mod root;
mod mounts;

pub mod api;
pub mod fops;
Expand Down
80 changes: 80 additions & 0 deletions modules/axfs/src/mounts.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
use alloc::sync::Arc;
use axfs_vfs::{VfsNodeType, VfsOps, VfsResult};

use crate::fs;

#[cfg(feature = "devfs")]
pub(crate) fn devfs() -> Arc<fs::devfs::DeviceFileSystem> {
let null = fs::devfs::NullDev;
let zero = fs::devfs::ZeroDev;
let bar = fs::devfs::ZeroDev;
let devfs = fs::devfs::DeviceFileSystem::new();
let foo_dir = devfs.mkdir("foo");
devfs.add("null", Arc::new(null));
devfs.add("zero", Arc::new(zero));
foo_dir.add("bar", Arc::new(bar));
Arc::new(devfs)
}

#[cfg(feature = "ramfs")]
pub(crate) fn ramfs() -> Arc<fs::ramfs::RamFileSystem> {
Arc::new(fs::ramfs::RamFileSystem::new())
}

#[cfg(feature = "procfs")]
pub(crate) fn procfs() -> VfsResult<Arc<fs::ramfs::RamFileSystem>> {
let procfs = fs::ramfs::RamFileSystem::new();
let proc_root = procfs.root_dir();

// Create /proc/sys/net/core/somaxconn
proc_root.create("sys", VfsNodeType::Dir)?;
proc_root.create("sys/net", VfsNodeType::Dir)?;
proc_root.create("sys/net/core", VfsNodeType::Dir)?;
proc_root.create("sys/net/core/somaxconn", VfsNodeType::File)?;
let file_somaxconn = proc_root.clone().lookup("./sys/net/core/somaxconn")?;
file_somaxconn.write_at(0, b"4096\n")?;

// Create /proc/sys/vm/overcommit_memory
proc_root.create("sys/vm", VfsNodeType::Dir)?;
proc_root.create("sys/vm/overcommit_memory", VfsNodeType::File)?;
let file_over = proc_root.clone().lookup("./sys/vm/overcommit_memory")?;
file_over.write_at(0, b"0\n")?;

// Create /proc/self/stat
proc_root.create("self", VfsNodeType::Dir)?;
proc_root.create("self/stat", VfsNodeType::File)?;

Ok(Arc::new(procfs))
}

#[cfg(feature = "sysfs")]
pub(crate) fn sysfs() -> VfsResult<Arc<fs::ramfs::RamFileSystem>> {
let sysfs = fs::ramfs::RamFileSystem::new();
let sys_root = sysfs.root_dir();

// Create /sys/kernel/mm/transparent_hugepage/enabled
sys_root.create("kernel", VfsNodeType::Dir)?;
sys_root.create("kernel/mm", VfsNodeType::Dir)?;
sys_root.create("kernel/mm/transparent_hugepage", VfsNodeType::Dir)?;
sys_root.create("kernel/mm/transparent_hugepage/enabled", VfsNodeType::File)?;
let file_hp = sys_root
.clone()
.lookup("./kernel/mm/transparent_hugepage/enabled")?;
file_hp.write_at(0, b"always [madvise] never\n")?;

// Create /sys/devices/system/clocksource/clocksource0/current_clocksource
sys_root.create("devices", VfsNodeType::Dir)?;
sys_root.create("devices/system", VfsNodeType::Dir)?;
sys_root.create("devices/system/clocksource", VfsNodeType::Dir)?;
sys_root.create("devices/system/clocksource/clocksource0", VfsNodeType::Dir)?;
sys_root.create(
"devices/system/clocksource/clocksource0/current_clocksource",
VfsNodeType::File,
)?;
let file_cc = sys_root
.clone()
.lookup("devices/system/clocksource/clocksource0/current_clocksource")?;
file_cc.write_at(0, b"tsc\n")?;

Ok(Arc::new(sysfs))
}
92 changes: 8 additions & 84 deletions modules/axfs/src/root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use axfs_vfs::{VfsNodeAttr, VfsNodeOps, VfsNodeRef, VfsNodeType, VfsOps, VfsResu
use axsync::Mutex;
use lazy_init::LazyInit;

use crate::{api::FileType, fs};
use crate::{api::FileType, fs, mounts};

static CURRENT_DIR_PATH: Mutex<String> = Mutex::new(String::new());
static CURRENT_DIR: LazyInit<Mutex<VfsNodeRef>> = LazyInit::new();
Expand Down Expand Up @@ -135,90 +135,14 @@ impl VfsNodeOps for RootDirectory {
fn rename(&self, src_path: &str, dst_path: &str) -> VfsResult {
self.lookup_mounted_fs(src_path, |fs, rest_path| {
if rest_path.is_empty() {
ax_err!(NotFound)
ax_err!(PermissionDenied) // cannot rename mount points
} else {
fs.root_dir().rename(rest_path, dst_path)
}
})
}
}

#[cfg(feature = "devfs")]
fn mount_devfs() -> fs::devfs::DeviceFileSystem {
let null = fs::devfs::NullDev;
let zero = fs::devfs::ZeroDev;
let bar = fs::devfs::ZeroDev;
let devfs = fs::devfs::DeviceFileSystem::new();
let foo_dir = devfs.mkdir("foo");
devfs.add("null", Arc::new(null));
devfs.add("zero", Arc::new(zero));
foo_dir.add("bar", Arc::new(bar));
devfs
}

#[cfg(feature = "ramfs")]
fn mount_ramfs() -> fs::ramfs::RamFileSystem {
fs::ramfs::RamFileSystem::new()
}

#[cfg(feature = "procfs")]
fn mount_procfs() -> VfsResult<fs::ramfs::RamFileSystem> {
let procfs = fs::ramfs::RamFileSystem::new();
let proc_root = procfs.root_dir();

// Create /proc/sys/net/core/somaxconn
proc_root.create("sys", VfsNodeType::Dir)?;
proc_root.create("sys/net", VfsNodeType::Dir)?;
proc_root.create("sys/net/core", VfsNodeType::Dir)?;
proc_root.create("sys/net/core/somaxconn", VfsNodeType::File)?;
let file_somaxconn = proc_root.clone().lookup("./sys/net/core/somaxconn")?;
file_somaxconn.write_at(0, b"4096")?;

// Create /proc/sys/vm/overcommit_memory
proc_root.create("sys/vm", VfsNodeType::Dir)?;
proc_root.create("sys/vm/overcommit_memory", VfsNodeType::File)?;
let file_over = proc_root.clone().lookup("./sys/vm/overcommit_memory")?;
file_over.write_at(0, b"0")?;

// Create /proc/self/stat
proc_root.create("self", VfsNodeType::Dir)?;
proc_root.create("self/stat", VfsNodeType::File)?;

Ok(procfs)
}

#[cfg(feature = "sysfs")]
fn mount_sysfs() -> VfsResult<fs::ramfs::RamFileSystem> {
let sysfs = fs::ramfs::RamFileSystem::new();
let sys_root = sysfs.root_dir();

// Create /sys/kernel/mm/transparent_hugepage/enabled
sys_root.create("kernel", VfsNodeType::Dir)?;
sys_root.create("kernel/mm", VfsNodeType::Dir)?;
sys_root.create("kernel/mm/transparent_hugepage", VfsNodeType::Dir)?;
sys_root.create("kernel/mm/transparent_hugepage/enabled", VfsNodeType::File)?;
let file_hp = sys_root
.clone()
.lookup("./kernel/mm/transparent_hugepage/enabled")?;
file_hp.write_at(0, b"always [madvise] never\n")?;

// Create /sys/devices/system/clocksource/clocksource0/current_clocksource
sys_root.create("devices", VfsNodeType::Dir)?;
sys_root.create("devices/system", VfsNodeType::Dir)?;
sys_root.create("devices/system/clocksource", VfsNodeType::Dir)?;
sys_root.create("devices/system/clocksource/clocksource0", VfsNodeType::Dir)?;
sys_root.create(
"devices/system/clocksource/clocksource0/current_clocksource",
VfsNodeType::File,
)?;
let file_cc = sys_root
.clone()
.lookup("devices/system/clocksource/clocksource0/current_clocksource")?;
file_cc.write_at(0, b"tsc\n")?;

Ok(sysfs)
}

pub(crate) fn init_rootfs(disk: crate::dev::Disk) {
cfg_if::cfg_if! {
if #[cfg(feature = "myfs")] { // override the default filesystem
Expand All @@ -235,24 +159,24 @@ pub(crate) fn init_rootfs(disk: crate::dev::Disk) {

#[cfg(feature = "devfs")]
root_dir
.mount("/dev", Arc::new(mount_devfs()))
.mount("/dev", mounts::devfs())
.expect("failed to mount devfs at /dev");

#[cfg(feature = "ramfs")]
root_dir
.mount("/tmp", Arc::new(mount_ramfs()))
.mount("/tmp", mounts::ramfs())
.expect("failed to mount ramfs at /tmp");

// Mount another ramfs as procfs
#[cfg(all(feature = "ramfs", feature = "procfs"))]
#[cfg(feature = "procfs")]
root_dir // should not fail
.mount("/proc", Arc::new(mount_procfs().unwrap()))
.mount("/proc", mounts::procfs().unwrap())
.expect("fail to mount procfs at /proc");

// Mount another ramfs as sysfs
#[cfg(all(feature = "ramfs", feature = "sysfs"))]
#[cfg(feature = "sysfs")]
root_dir // should not fail
.mount("/sys", Arc::new(mount_sysfs().unwrap()))
.mount("/sys", mounts::sysfs().unwrap())
.expect("fail to mount sysfs at /sys");

ROOT_DIR.init_by(Arc::new(root_dir));
Expand Down
16 changes: 3 additions & 13 deletions ulib/axlibc/c/network.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ void freeaddrinfo(struct addrinfo *__restrict res)
return;
}

static const char msgs_lc[] = "Invalid flags\0"
static const char gai_msgs[] = "Invalid flags\0"
"Name does not resolve\0"
"Try again\0"
"Non-recoverable error\0"
Expand All @@ -61,22 +61,15 @@ static const char msgs_lc[] = "Invalid flags\0"
"Overflow\0"
"\0Unknown error";

const char *__lctrans_cur(const char *msg)
{
return msg;
}

#define LCTRANS_CUR(msg) __lctrans_cur(msg)

const char *gai_strerror(int ecode)
{
const char *s;
for (s = msgs_lc, ecode++; ecode && *s; ecode++, s++)
for (s = gai_msgs, ecode++; ecode && *s; ecode++, s++)
for (; *s; s++)
;
if (!*s)
s++;
return LCTRANS_CUR(s);
return s;
}

static const char msgs[] = "Host not found\0"
Expand Down Expand Up @@ -106,9 +99,6 @@ static __inline uint32_t __bswap_32(uint32_t __x)
return __x >> 24 | (__x >> 8 & 0xff00) | (__x << 8 & 0xff0000) | __x << 24;
}

#define bswap_16(x) __bswap_16(x)
#define bswap_32(x) __bswap_32(x)

uint32_t htonl(uint32_t n)
{
union {
Expand Down
11 changes: 4 additions & 7 deletions ulib/axlibc/c/pthread.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifdef AX_CONFIG_MULTITASK

#include <errno.h>
#include <axlibc.h>
#include <errno.h>
#include <limits.h>
#include <pthread.h>
#include <unistd.h>
Expand Down Expand Up @@ -55,20 +55,17 @@ int pthread_cancel(pthread_t t)

int pthread_mutex_init(pthread_mutex_t *restrict m, const pthread_mutexattr_t *restrict a)
{
ax_pthread_mutex_init(m, a);
return 0;
return ax_pthread_mutex_init(m, a);
}

int pthread_mutex_lock(pthread_mutex_t *m)
{
ax_pthread_mutex_lock(m);
return 0;
return ax_pthread_mutex_lock(m);
}

int pthread_mutex_unlock(pthread_mutex_t *m)
{
ax_pthread_mutex_unlock(m);
return 0;
return ax_pthread_mutex_unlock(m);
}

// TODO
Expand Down
5 changes: 3 additions & 2 deletions ulib/axlibc/src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use core::ffi::{c_char, c_int};

use axerrno::{LinuxError, LinuxResult};
use axio::{prelude::*, PollState, SeekFrom};
use axstd::fs::{self, OpenOptions};
use axstd::fs::OpenOptions;
use axstd::sync::Mutex;

use crate::{ctypes, fd_ops::FileLike, utils::char_ptr_to_str};
Expand Down Expand Up @@ -203,6 +203,7 @@ pub unsafe extern "C" fn ax_rename(old: *const c_char, new: *const c_char) -> c_
let old_path = char_ptr_to_str(old)?;
let new_path = char_ptr_to_str(new)?;
debug!("ax_rename <= old: {:?}, new: {:?}", old_path, new_path);
fs::rename(old_path, new_path).map_or_else(|e| Err(LinuxError::from(e)), |_| Ok(0))
axstd::fs::rename(old_path, new_path)?;
Ok(0)
})
}

0 comments on commit 9c4cf10

Please sign in to comment.