From ab7616f1845f867a7d8713e3ac7f8ffc72148349 Mon Sep 17 00:00:00 2001 From: zyfjeff Date: Thu, 11 Apr 2024 17:41:22 +0800 Subject: [PATCH] Filter out O_DIRECT at open_inode Filter out the libc::O_DIRECT flag at open_inode. Also, add the "allow_direct_io" flag to allow users to make the file system to honor the flag instead. reference virtiofsd 894361d1e83c23460ce9aaadc40a6af598a790d4 Signed-off-by: zyfjeff --- src/passthrough/config.rs | 7 +++++++ src/passthrough/sync_io.rs | 5 ++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/passthrough/config.rs b/src/passthrough/config.rs index bf2c101ed..dd7fb951f 100644 --- a/src/passthrough/config.rs +++ b/src/passthrough/config.rs @@ -175,6 +175,12 @@ pub struct Config { /// is bigger than the threshold. /// The default value for this option is `false`. pub use_host_ino: bool, + + /// Whether the file system should honor the O_DIRECT flag. If this option is disabled, + /// that flag will be filtered out at `open_inode`. + /// + /// The default is `true`. + pub allow_direct_io: bool, } impl Default for Config { @@ -198,6 +204,7 @@ impl Default for Config { dir_entry_timeout: None, dir_attr_timeout: None, use_host_ino: false, + allow_direct_io: true, } } } diff --git a/src/passthrough/sync_io.rs b/src/passthrough/sync_io.rs index 925bc2b2f..76520efe6 100644 --- a/src/passthrough/sync_io.rs +++ b/src/passthrough/sync_io.rs @@ -34,7 +34,10 @@ impl PassthroughFs { if !is_safe_inode(data.mode) { Err(ebadf()) } else { - let new_flags = self.get_writeback_open_flags(flags); + let mut new_flags = self.get_writeback_open_flags(flags); + if !self.cfg.allow_direct_io && flags & libc::O_DIRECT != 0 { + new_flags &= !libc::O_DIRECT; + } data.open_file(new_flags | libc::O_CLOEXEC, &self.proc_self_fd) } }