Skip to content

Fix: unable to perform sync on directory #205

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

Merged
merged 2 commits into from
Apr 14, 2025
Merged
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
4 changes: 2 additions & 2 deletions src/api/filesystem/overlay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,8 @@ pub(crate) fn is_chardev(st: stat64) -> bool {
pub(crate) fn is_whiteout(st: stat64) -> bool {
// A whiteout is created as a character device with 0/0 device number.
// See ref: https://docs.kernel.org/filesystems/overlayfs.html#whiteouts-and-opaque-directories
let major = unsafe { libc::major(st.st_rdev) };
let minor = unsafe { libc::minor(st.st_rdev) };
let major = libc::major(st.st_rdev);
let minor = libc::minor(st.st_rdev);
is_chardev(st) && major == 0 && minor == 0
}

Expand Down
8 changes: 2 additions & 6 deletions src/api/vfs/sync_io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,9 +306,7 @@ impl FileSystem for Vfs {
}
match self.get_real_rootfs(inode)? {
(Left(fs), idata) => fs.open(ctx, idata.ino(), flags, fuse_flags),
(Right(fs), idata) => fs
.open(ctx, idata.ino(), flags, fuse_flags)
.map(|(h, opt, passthrough)| (h.map(Into::into), opt, passthrough)),
(Right(fs), idata) => fs.open(ctx, idata.ino(), flags, fuse_flags),
}
}

Expand Down Expand Up @@ -522,9 +520,7 @@ impl FileSystem for Vfs {
}
match self.get_real_rootfs(inode)? {
(Left(fs), idata) => fs.opendir(ctx, idata.ino(), flags),
(Right(fs), idata) => fs
.opendir(ctx, idata.ino(), flags)
.map(|(h, opt)| (h.map(Into::into), opt)),
(Right(fs), idata) => fs.opendir(ctx, idata.ino(), flags),
}
}

Expand Down
32 changes: 15 additions & 17 deletions src/passthrough/sync_io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use std::sync::Arc;
use std::time::Duration;

use super::os_compat::LinuxDirent64;
use super::util::stat_fd;
use super::util::{stat_fd, sync_fd};
use super::*;
use crate::abi::fuse_abi::{CreateIn, Opcode, FOPEN_IN_KILL_SUIDGID, WRITE_KILL_PRIV};
#[cfg(any(feature = "vhost-user-fs", feature = "virtiofs"))]
Expand Down Expand Up @@ -1074,30 +1074,19 @@ impl<S: BitmapSlice + Send + Sync> FileSystem for PassthroughFs<S> {
) -> io::Result<()> {
let data = self.get_data(handle, inode, libc::O_RDONLY)?;
let fd = data.borrow_fd();

// Safe because this doesn't modify any memory and we check the return value.
let res = unsafe {
if datasync {
libc::fdatasync(fd.as_raw_fd())
} else {
libc::fsync(fd.as_raw_fd())
}
};
if res == 0 {
Ok(())
} else {
Err(io::Error::last_os_error())
}
sync_fd(&fd, datasync)
}

fn fsyncdir(
&self,
ctx: &Context,
_ctx: &Context,
inode: Inode,
datasync: bool,
handle: Handle,
) -> io::Result<()> {
self.fsync(ctx, inode, datasync, handle)
let data = self.get_dirdata(handle, inode, libc::O_RDONLY)?;
let fd = data.borrow_fd();
sync_fd(&fd, datasync)
}

fn access(&self, ctx: &Context, inode: Inode, mask: u32) -> io::Result<()> {
Expand Down Expand Up @@ -1630,4 +1619,13 @@ mod tests {
let statfs = fs.statfs(&ctx, ROOT_ID).unwrap();
assert_eq!(statfs.f_namemax, 255);
}

#[test]
fn test_fsync_dir() {
let (fs, _source) = prepare_fs_tmpdir();
let ctx = prepare_context();
fs.no_opendir.store(true, Ordering::Relaxed);

assert!(fs.fsyncdir(&ctx, ROOT_ID, false, 0).is_ok());
}
}
16 changes: 16 additions & 0 deletions src/passthrough/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,22 @@ pub fn eperm() -> io::Error {
io::Error::from_raw_os_error(libc::EPERM)
}

pub fn sync_fd(fd: &impl AsRawFd, datasync: bool) -> io::Result<()> {
// Safe because this doesn't modify any memory and we check the return value.
let res = unsafe {
if datasync {
libc::fdatasync(fd.as_raw_fd())
} else {
libc::fsync(fd.as_raw_fd())
}
};
if res == 0 {
Ok(())
} else {
Err(io::Error::last_os_error())
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
2 changes: 1 addition & 1 deletion src/transport/fusedev/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ impl<'a, S: BitmapSlice> FuseDevWriter<'a, S> {
}

fn check_available_space(&self, sz: usize) -> io::Result<()> {
assert!(self.buffered || self.buf.len() == 0);
assert!(self.buffered || self.buf.is_empty());
if sz > self.available_bytes() {
Err(io::Error::new(
io::ErrorKind::InvalidData,
Expand Down
Loading