This repository was archived by the owner on Nov 26, 2025. It is now read-only.
forked from Azure-stars/starry-next
-
Notifications
You must be signed in to change notification settings - Fork 36
Implement mount, umount for basic testcases #21
Merged
Merged
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e32356a
implement mount
Ressed 59684d3
format
Ressed e23c6f8
translate comment
Ressed 015dc9b
Merge branch 'oscomp:main' into impl_mount
Ressed 8bfaf32
mount, umount revision
Ressed 4e307c3
mount, umount revision
Ressed 49be91d
fix clippy
Ressed File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,13 @@ | ||
| mod ctl; | ||
| mod fd_ops; | ||
| mod io; | ||
| mod mount; | ||
| mod pipe; | ||
| mod stat; | ||
|
|
||
| pub(crate) use self::ctl::*; | ||
| pub(crate) use self::fd_ops::*; | ||
| pub(crate) use self::io::*; | ||
| pub(crate) use self::mount::*; | ||
| pub(crate) use self::pipe::*; | ||
| pub(crate) use self::stat::*; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,142 @@ | ||
| use alloc::vec::Vec; | ||
| use arceos_posix_api::{AT_FDCWD, FilePath, handle_file_path}; | ||
| use axerrno::{LinuxError, LinuxResult}; | ||
| use axsync::Mutex; | ||
| use core::ffi::{c_char, c_void}; | ||
|
|
||
| use crate::ptr::UserConstPtr; | ||
|
|
||
| pub fn sys_mount( | ||
| source: UserConstPtr<c_char>, | ||
| target: UserConstPtr<c_char>, | ||
| fs_type: UserConstPtr<c_char>, | ||
| _flags: i32, | ||
| _data: *const c_void, | ||
| ) -> LinuxResult<isize> { | ||
| info!("sys_mount"); | ||
| let source = source.get_as_null_terminated()?; | ||
| let target = target.get_as_null_terminated()?; | ||
| let fs_type = fs_type.get_as_str()?; | ||
| let device_path = handle_file_path(AT_FDCWD, Some(source.as_ptr() as _), false)?; | ||
| let mount_path = handle_file_path(AT_FDCWD, Some(target.as_ptr() as _), true)?; | ||
| info!( | ||
| "mount {:?} to {:?} with fs_type={:?}", | ||
| device_path, mount_path, fs_type | ||
| ); | ||
|
|
||
| if fs_type != "vfat" { | ||
| debug!("fs_type can only be vfat."); | ||
| return Err(LinuxError::EPERM); | ||
| } | ||
|
|
||
| if !mount_path.exists() { | ||
| debug!("mount path not exist"); | ||
| return Err(LinuxError::EPERM); | ||
| } | ||
|
|
||
| if check_mounted(&mount_path) { | ||
| debug!("mount path includes mounted fs"); | ||
| return Err(LinuxError::EPERM); | ||
| } | ||
|
|
||
| if !mount_fat_fs(&device_path, &mount_path) { | ||
| debug!("mount error"); | ||
| return Err(LinuxError::EPERM); | ||
| } | ||
| Ok(0) | ||
| } | ||
|
|
||
| pub fn sys_umount2(target: UserConstPtr<c_char>, flags: i32) -> LinuxResult<isize> { | ||
| info!("sys_umount2"); | ||
| let target = target.get_as_null_terminated()?; | ||
| let mount_path = handle_file_path(AT_FDCWD, Some(target.as_ptr() as _), true)?; | ||
| if flags != 0 { | ||
| debug!("flags unimplemented"); | ||
| return Err(LinuxError::EPERM); | ||
| } | ||
|
|
||
| if !mount_path.exists() { | ||
| debug!("mount path not exist"); | ||
| return Err(LinuxError::EPERM); | ||
| } | ||
|
|
||
| if !umount_fat_fs(&mount_path) { | ||
| debug!("umount error"); | ||
| return Err(LinuxError::EPERM); | ||
| } | ||
| Ok(0) | ||
| } | ||
|
|
||
| /// Mounted File System | ||
| /// "Mount" means read&write a file as a file system now | ||
| pub struct MountedFs { | ||
| //pub inner: Arc<Mutex<FATFileSystem>>, | ||
| pub device: FilePath, | ||
| pub mnt_dir: FilePath, | ||
| } | ||
|
|
||
| impl MountedFs { | ||
| pub fn new(device: &FilePath, mnt_dir: &FilePath) -> Self { | ||
| assert!( | ||
| device.is_file() && mnt_dir.is_dir(), | ||
| "device must be a file and mnt_dir must be a dir" | ||
| ); | ||
| Self { | ||
| device: device.clone(), | ||
| mnt_dir: mnt_dir.clone(), | ||
| } | ||
| } | ||
| #[allow(unused)] | ||
| pub fn device(&self) -> FilePath { | ||
| self.device.clone() | ||
| } | ||
|
|
||
| pub fn mnt_dir(&self) -> FilePath { | ||
| self.mnt_dir.clone() | ||
| } | ||
| } | ||
|
|
||
| /// List of mounted file system | ||
| /// Note that the startup file system is not in the vec, but in mod.rs | ||
| static MOUNTED: Mutex<Vec<MountedFs>> = Mutex::new(Vec::new()); | ||
|
|
||
| /// Mount a fatfs device | ||
| pub fn mount_fat_fs(device_path: &FilePath, mount_path: &FilePath) -> bool { | ||
| // device_path needs symlink lookup, but mount_path does not | ||
| // only opened files will be added to the symlink table for now, so do not convert now | ||
| // debug!("mounting {} to {}", device_path.path(), mount_path.path()); | ||
| // if let Some(true_device_path) = real_path(device_path) { | ||
| if mount_path.exists() { | ||
| MOUNTED.lock().push(MountedFs::new(device_path, mount_path)); | ||
| info!( | ||
| "mounted {} to {}", | ||
| device_path.as_str(), | ||
| mount_path.as_str() | ||
| ); | ||
| return true; | ||
| } | ||
| info!( | ||
| "mount failed: {} to {}", | ||
| device_path.as_str(), | ||
| mount_path.as_str() | ||
| ); | ||
| false | ||
| } | ||
|
|
||
| /// unmount a fatfs device | ||
| pub fn umount_fat_fs(mount_path: &FilePath) -> bool { | ||
| let mut mounted = MOUNTED.lock(); | ||
| let length_before_deletion = mounted.len(); | ||
| mounted.retain(|m| m.mnt_dir() != *mount_path); | ||
| if length_before_deletion > mounted.len() { | ||
|
Ressed marked this conversation as resolved.
Outdated
|
||
| return true; | ||
| } | ||
| info!("umount failed: {}", mount_path.as_str()); | ||
| false | ||
| } | ||
|
|
||
| /// check if a path is mounted | ||
| pub fn check_mounted(path: &FilePath) -> bool { | ||
| let mounted = MOUNTED.lock(); | ||
| mounted.iter().any(|m| path.starts_with(&m.mnt_dir())) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.