Add sys_fdatasync and sync_data

This commit is contained in:
Fabing Li
2024-06-27 10:46:58 +08:00
committed by Tate, Hongliang Tian
parent 5edc110f9d
commit 212dd562a0
18 changed files with 178 additions and 81 deletions

View File

@ -135,7 +135,7 @@ impl ExfatFS {
if inode.is_deleted() {
inode.reclaim_space()?;
} else {
inode.sync()?;
inode.sync_all()?;
}
}
self.inodes.write().remove(&hash);
@ -392,7 +392,7 @@ impl PageCacheBackend for ExfatFS {
impl FileSystem for ExfatFS {
fn sync(&self) -> Result<()> {
for inode in self.inodes.read().values() {
inode.sync()?;
inode.sync_all()?;
}
self.meta_cache.evict_range(0..self.fs_size())?;
Ok(())

View File

@ -573,9 +573,14 @@ impl ExfatInodeInner {
Ok(())
}
fn sync(&self, fs_guard: &MutexGuard<()>) -> Result<()> {
fn sync_data(&self, fs_guard: &MutexGuard<()>) -> Result<()> {
self.page_cache.evict_range(0..self.size)?;
Ok(())
}
fn sync_all(&self, fs_guard: &MutexGuard<()>) -> Result<()> {
self.sync_metadata(fs_guard)?;
self.sync_data(fs_guard)?;
Ok(())
}
@ -1328,7 +1333,7 @@ impl Inode for ExfatInode {
if inner.is_sync() {
let fs = inner.fs();
let fs_guard = fs.lock();
inner.sync(&fs_guard)?;
inner.sync_all(&fs_guard)?;
}
Ok(buf.len())
@ -1435,7 +1440,7 @@ impl Inode for ExfatInode {
let inner = self.inner.read();
if inner.is_sync() {
inner.sync(&fs_guard)?;
inner.sync_all(&fs_guard)?;
}
Ok(result)
@ -1530,7 +1535,7 @@ impl Inode for ExfatInode {
let inner = self.inner.read();
if inner.is_sync() {
inner.sync(&fs_guard)?;
inner.sync_all(&fs_guard)?;
}
Ok(())
@ -1567,7 +1572,7 @@ impl Inode for ExfatInode {
let inner = self.inner.read();
// Sync this inode since size has changed.
if inner.is_sync() {
inner.sync(&fs_guard)?;
inner.sync_all(&fs_guard)?;
}
Ok(())
@ -1656,9 +1661,9 @@ impl Inode for ExfatInode {
// Sync
if self.inner.read().is_sync() || target_.inner.read().is_sync() {
// TODO: what if fs crashed between syncing?
old_inode.inner.read().sync(&fs_guard)?;
target_.inner.read().sync(&fs_guard)?;
self.inner.read().sync(&fs_guard)?;
old_inode.inner.read().sync_all(&fs_guard)?;
target_.inner.read().sync_all(&fs_guard)?;
self.inner.read().sync_all(&fs_guard)?;
}
Ok(())
}
@ -1675,11 +1680,20 @@ impl Inode for ExfatInode {
return_errno_with_message!(Errno::EINVAL, "unsupported operation")
}
fn sync(&self) -> Result<()> {
fn sync_all(&self) -> Result<()> {
let inner = self.inner.read();
let fs = inner.fs();
let fs_guard = fs.lock();
inner.sync(&fs_guard)?;
inner.sync_all(&fs_guard)?;
Ok(())
}
fn sync_data(&self) -> Result<()> {
let inner = self.inner.read();
let fs = inner.fs();
let fs_guard = fs.lock();
inner.sync_data(&fs_guard)?;
Ok(())
}

View File

@ -176,10 +176,14 @@ impl Inode for Ext2Inode {
Err(Error::new(Errno::EINVAL))
}
fn sync(&self) -> Result<()> {
fn sync_all(&self) -> Result<()> {
self.sync_all()
}
fn sync_data(&self) -> Result<()> {
self.sync_data()
}
fn fs(&self) -> Arc<dyn FileSystem> {
self.fs()
}

View File

@ -299,7 +299,8 @@ impl Dentry_ {
#[inherit_methods(from = "self.inode")]
impl Dentry_ {
pub fn fs(&self) -> Arc<dyn FileSystem>;
pub fn sync(&self) -> Result<()>;
pub fn sync_all(&self) -> Result<()>;
pub fn sync_data(&self) -> Result<()>;
pub fn metadata(&self) -> Metadata;
pub fn type_(&self) -> InodeType;
pub fn mode(&self) -> Result<InodeMode>;
@ -680,7 +681,8 @@ impl Dentry {
#[inherit_methods(from = "self.inner")]
impl Dentry {
pub fn fs(&self) -> Arc<dyn FileSystem>;
pub fn sync(&self) -> Result<()>;
pub fn sync_all(&self) -> Result<()>;
pub fn sync_data(&self) -> Result<()>;
pub fn metadata(&self) -> Metadata;
pub fn type_(&self) -> InodeType;
pub fn mode(&self) -> Result<InodeMode>;

View File

@ -183,10 +183,6 @@ impl<D: DirOps + 'static> Inode for ProcDir<D> {
Err(Error::new(Errno::EPERM))
}
fn sync(&self) -> Result<()> {
Ok(())
}
fn is_dentry_cacheable(&self) -> bool {
!self.common.is_volatile()
}

View File

@ -95,10 +95,6 @@ impl<F: FileOps + 'static> Inode for ProcFile<F> {
Err(Error::new(Errno::EPERM))
}
fn sync(&self) -> Result<()> {
Ok(())
}
fn is_dentry_cacheable(&self) -> bool {
!self.common.is_volatile()
}

View File

@ -87,10 +87,6 @@ impl<S: SymOps + 'static> Inode for ProcSym<S> {
Err(Error::new(Errno::EPERM))
}
fn sync(&self) -> Result<()> {
Ok(())
}
fn is_dentry_cacheable(&self) -> bool {
!self.common.is_volatile()
}

View File

@ -993,11 +993,6 @@ impl Inode for RamInode {
}
}
fn sync(&self) -> Result<()> {
// do nothing
Ok(())
}
fn poll(&self, mask: IoEvents, poller: Option<&Poller>) -> IoEvents {
if let Some(device) = self.node.read().inner.as_device() {
device.poll(mask, poller)

View File

@ -340,7 +340,11 @@ pub trait Inode: Any + Sync + Send {
Err(Error::new(Errno::EISDIR))
}
fn sync(&self) -> Result<()> {
fn sync_all(&self) -> Result<()> {
Ok(())
}
fn sync_data(&self) -> Result<()> {
Ok(())
}

View File

@ -25,7 +25,7 @@ use crate::syscall::{
exit_group::sys_exit_group,
fcntl::sys_fcntl,
fork::sys_fork,
fsync::sys_fsync,
fsync::{sys_fdatasync, sys_fsync},
futex::sys_futex,
getcwd::sys_getcwd,
getdents64::{sys_getdents, sys_getdents64},
@ -182,6 +182,7 @@ impl_syscall_nums_and_dispatch_fn! {
SYS_UNAME = 63 => sys_uname(args[..1]);
SYS_FCNTL = 72 => sys_fcntl(args[..3]);
SYS_FSYNC = 74 => sys_fsync(args[..1]);
SYS_FDATASYNC = 75 => sys_fdatasync(args[..1]);
SYS_TRUNCATE = 76 => sys_truncate(args[..2]);
SYS_FTRUNCATE = 77 => sys_ftruncate(args[..2]);
SYS_GETDENTS = 78 => sys_getdents(args[..3]);

View File

@ -18,6 +18,22 @@ pub fn sys_fsync(fd: FileDesc) -> Result<SyscallReturn> {
.ok_or(Error::with_message(Errno::EINVAL, "not inode"))?;
inode_handle.dentry().clone()
};
dentry.sync()?;
dentry.sync_all()?;
Ok(SyscallReturn::Return(0))
}
pub fn sys_fdatasync(fd: FileDesc) -> Result<SyscallReturn> {
debug!("fd = {}", fd);
let dentry = {
let current = current!();
let file_table = current.file_table().lock();
let file = file_table.get_file(fd)?;
let inode_handle = file
.downcast_ref::<InodeHandle>()
.ok_or(Error::with_message(Errno::EINVAL, "not inode"))?;
inode_handle.dentry().clone()
};
dentry.sync_data()?;
Ok(SyscallReturn::Return(0))
}