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

add file flush in sys_close and sys_exit #80

Closed
wants to merge 1 commit 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
15 changes: 14 additions & 1 deletion api/ruxos_posix_api/src/imp/fd_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pub const RUX_FILE_LIMIT: usize = 1024;
pub trait FileLike: Send + Sync {
fn read(&self, buf: &mut [u8]) -> LinuxResult<usize>;
fn write(&self, buf: &[u8]) -> LinuxResult<usize>;
fn flush(&self) -> LinuxResult;
fn stat(&self) -> LinuxResult<ctypes::stat>;
fn into_any(self: Arc<Self>) -> Arc<dyn core::any::Any + Send + Sync>;
fn poll(&self) -> LinuxResult<PollState>;
Expand Down Expand Up @@ -61,13 +62,25 @@ pub fn close_file_like(fd: c_int) -> LinuxResult {
Ok(())
}

pub fn flush_file_like(fd: c_int) -> LinuxResult {
let f = get_file_like(fd)?;
f.flush()
coolyjg marked this conversation as resolved.
Show resolved Hide resolved
}

pub fn fd_table_count() -> usize {
FD_TABLE.read().count()
}

/// Close a file by `fd`.
pub fn sys_close(fd: c_int) -> c_int {
debug!("sys_close <= {}", fd);
if (0..=2).contains(&fd) {
return 0; // stdin, stdout, stderr
}
syscall_body!(sys_close, close_file_like(fd).map(|_| 0))
syscall_body!(sys_close, {
get_file_like(fd)?.flush()?;
close_file_like(fd).map(|_| 0)
})
}

fn dup_fd(old_fd: c_int) -> LinuxResult<c_int> {
Expand Down
12 changes: 10 additions & 2 deletions api/ruxos_posix_api/src/imp/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ impl FileLike for File {
Ok(self.inner.lock().write(buf)?)
}

fn flush(&self) -> LinuxResult {
Ok(self.inner.lock().flush()?)
}

fn stat(&self) -> LinuxResult<ctypes::stat> {
let metadata = self.inner.lock().get_attr()?;
let ty = metadata.file_type() as u8;
Expand Down Expand Up @@ -117,6 +121,10 @@ impl FileLike for Directory {
Err(LinuxError::EACCES)
}

fn flush(&self) -> LinuxResult {
Ok(())
}

fn stat(&self) -> LinuxResult<ctypes::stat> {
let metadata = self.inner.lock().get_attr()?;
let ty = metadata.file_type() as u8;
Expand Down Expand Up @@ -184,7 +192,7 @@ fn flags_to_options(flags: c_int, _mode: ctypes::mode_t) -> OpenOptions {
/// has the maximum number of files open.
pub fn sys_open(filename: *const c_char, flags: c_int, mode: ctypes::mode_t) -> c_int {
let filename = char_ptr_to_str(filename);
debug!("sys_open <= {:?} {:#o} {:#o}", filename, flags, mode);
info!("sys_open <= {:?} {:#o} {:#o}", filename, flags, mode);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use debug

syscall_body!(sys_open, {
let options = flags_to_options(flags, mode);
let file = ruxfs::fops::File::open(filename?, &options)?;
Expand All @@ -196,7 +204,7 @@ pub fn sys_open(filename: *const c_char, flags: c_int, mode: ctypes::mode_t) ->
pub fn sys_openat(fd: usize, path: *const c_char, flags: c_int, mode: ctypes::mode_t) -> c_int {
let path = char_ptr_to_str(path);
let fd: c_int = fd as c_int;
debug!("sys_openat <= {}, {:?}, {:#o} {:#o}", fd, path, flags, mode);
info!("sys_openat <= {}, {:?}, {:#o} {:#o}", fd, path, flags, mode);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use debug

syscall_body!(sys_openat, {
let options = flags_to_options(flags, mode);
if (flags as u32) & ctypes::O_DIRECTORY != 0 {
Expand Down
4 changes: 4 additions & 0 deletions api/ruxos_posix_api/src/imp/io_mpx/epoll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ impl FileLike for EpollInstance {
Err(LinuxError::ENOSYS)
}

fn flush(&self) -> LinuxResult {
Ok(())
}

fn stat(&self) -> LinuxResult<ctypes::stat> {
let st_mode = 0o600u32; // rw-------
Ok(ctypes::stat {
Expand Down
5 changes: 5 additions & 0 deletions api/ruxos_posix_api/src/imp/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,11 @@ impl FileLike for Socket {
self.send(buf)
}

///TODO
fn flush(&self) -> LinuxResult {
Ok(())
coolyjg marked this conversation as resolved.
Show resolved Hide resolved
}

fn stat(&self) -> LinuxResult<ctypes::stat> {
// not really implemented
let st_mode = 0o140000 | 0o777u32; // S_IFSOCK | rwxrwxrwx
Expand Down
4 changes: 4 additions & 0 deletions api/ruxos_posix_api/src/imp/pipe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,10 @@ impl FileLike for Pipe {
}
}

fn flush(&self) -> LinuxResult {
Ok(())
}

fn stat(&self) -> LinuxResult<ctypes::stat> {
let st_mode = 0o10000 | 0o600u32; // S_IFIFO | rw-------
Ok(ctypes::stat {
Expand Down
17 changes: 17 additions & 0 deletions api/ruxos_posix_api/src/imp/pthread/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ use ruxtask::AxTaskRef;
use spin::RwLock;

use crate::ctypes;
#[cfg(feature = "fd")]
use crate::imp::fd_ops::{fd_table_count, flush_file_like, RUX_FILE_LIMIT};

pub mod condvar;
pub mod mutex;
Expand Down Expand Up @@ -208,6 +210,21 @@ pub unsafe fn sys_pthread_create(
/// Exits the current thread. The value `retval` will be returned to the joiner.
pub fn sys_pthread_exit(retval: *mut c_void) -> ! {
debug!("sys_pthread_exit <= {:#x}", retval as usize);
#[cfg(feature = "fd")]
{
let mut now_fd_count = 0;
let mut fd_index: usize = 0;
while fd_index < RUX_FILE_LIMIT {
let flush_res = flush_file_like(fd_index.try_into().unwrap());
if flush_res == Ok(()) {
now_fd_count += 1;
if now_fd_count == fd_table_count() {
break;
}
}
fd_index += 1;
}
}
coolyjg marked this conversation as resolved.
Show resolved Hide resolved
#[cfg(feature = "musl")]
{
use core::sync::atomic::Ordering;
Expand Down
9 changes: 9 additions & 0 deletions api/ruxos_posix_api/src/imp/stdio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ impl super::fd_ops::FileLike for Stdin {
Err(LinuxError::EPERM)
}

///TODO
fn flush(&self) -> LinuxResult {
Ok(())
}

fn stat(&self) -> LinuxResult<crate::ctypes::stat> {
let st_mode = 0o20000 | 0o440u32; // S_IFCHR | r--r-----
Ok(crate::ctypes::stat {
Expand Down Expand Up @@ -156,6 +161,10 @@ impl super::fd_ops::FileLike for Stdout {
Ok(self.inner.lock().write(buf)?)
}

fn flush(&self) -> LinuxResult {
Ok(())
coolyjg marked this conversation as resolved.
Show resolved Hide resolved
}

fn stat(&self) -> LinuxResult<crate::ctypes::stat> {
let st_mode = 0o20000 | 0o220u32; // S_IFCHR | -w--w----
Ok(crate::ctypes::stat {
Expand Down
17 changes: 17 additions & 0 deletions api/ruxos_posix_api/src/imp/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
* See the Mulan PSL v2 for more details.
*/

#[cfg(feature = "fd")]
use crate::imp::fd_ops::{fd_table_count, flush_file_like, RUX_FILE_LIMIT};
use core::ffi::c_int;

/// Relinquish the CPU, and switches to another task.
Expand Down Expand Up @@ -42,6 +44,21 @@ pub fn sys_getpid() -> c_int {
/// Exit current task
pub fn sys_exit(exit_code: c_int) -> ! {
debug!("sys_exit <= {}", exit_code);
#[cfg(feature = "fd")]
{
let mut now_fd_count = 0;
let mut fd_index: usize = 0;
while fd_index < RUX_FILE_LIMIT {
let flush_res = flush_file_like(fd_index.try_into().unwrap());
if flush_res == Ok(()) {
now_fd_count += 1;
if now_fd_count == fd_table_count() {
break;
}
}
fd_index += 1;
}
}
coolyjg marked this conversation as resolved.
Show resolved Hide resolved
#[cfg(feature = "multitask")]
ruxtask::exit(exit_code);
#[cfg(not(feature = "multitask"))]
Expand Down
5 changes: 5 additions & 0 deletions modules/ruxfs/src/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,9 @@ impl Disk {
};
Ok(write_size)
}

///flush device cache
pub fn do_flush(&mut self) -> DevResult {
self.dev.flush()
}
}
7 changes: 6 additions & 1 deletion modules/ruxfs/src/fs/fatfs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/

use alloc::sync::Arc;
use axio::Error;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unused?

use core::cell::UnsafeCell;

use axfs_vfs::{VfsDirEntry, VfsError, VfsNodePerm, VfsResult};
Expand Down Expand Up @@ -74,6 +75,10 @@ impl FatFileSystem {
impl VfsNodeOps for FileWrapper<'static> {
axfs_vfs::impl_vfs_non_dir_default! {}

fn fsync(&self) -> VfsResult {
self.0.lock().flush().map_err(as_vfs_err)
}

fn get_attr(&self) -> VfsResult<VfsNodeAttr> {
let size = self.0.lock().seek(SeekFrom::End(0)).map_err(as_vfs_err)?;
let blocks = (size + BLOCK_SIZE as u64 - 1) / BLOCK_SIZE as u64;
Expand Down Expand Up @@ -272,7 +277,7 @@ impl Write for Disk {
Ok(write_len)
}
fn flush(&mut self) -> Result<(), Self::Error> {
Ok(())
self.do_flush().map_err(|_| ())
}
}

Expand Down
Loading