From 6e88bd8edba4eddef6b198a5274a8234b043fa6f Mon Sep 17 00:00:00 2001 From: akitaSummer Date: Thu, 25 Jan 2024 10:22:58 +0800 Subject: [PATCH] fix: fix cr Signed-off-by: akitaSummer --- src/passthrough/file_handle.rs | 2 -- src/passthrough/passthrough_fs_linux.rs | 11 +++--- src/passthrough/passthrough_fs_macos.rs | 7 ++-- src/passthrough/sync_io.rs | 46 +++++++++++++++++++++++-- src/passthrough/sync_io_linux.rs | 33 +----------------- src/passthrough/sync_io_macos.rs | 35 +------------------ 6 files changed, 57 insertions(+), 77 deletions(-) diff --git a/src/passthrough/file_handle.rs b/src/passthrough/file_handle.rs index fa5379470..5eb00c282 100644 --- a/src/passthrough/file_handle.rs +++ b/src/passthrough/file_handle.rs @@ -320,10 +320,8 @@ impl OpenableFileHandle { #[cfg(test)] mod tests { use super::*; - #[cfg(target_os = "macos")] use nix::unistd::getuid; use std::ffi::CString; - #[cfg(target_os = "macos")] use std::io::Read; fn generate_c_file_handle( diff --git a/src/passthrough/passthrough_fs_linux.rs b/src/passthrough/passthrough_fs_linux.rs index 306fa6457..d57c31876 100644 --- a/src/passthrough/passthrough_fs_linux.rs +++ b/src/passthrough/passthrough_fs_linux.rs @@ -1,3 +1,8 @@ +// Copyright (C) 2023 Alibaba Cloud. All rights reserved. +// Copyright 2021 Red Hat, Inc. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE-BSD-3-Clause file. + use std::{ ffi::{CStr, OsString}, fs::File, @@ -25,7 +30,7 @@ use super::{ pub type InoT = libc::ino64_t; pub type InodeMode = u32; -pub type LibCStat = libc::stat64; +pub type LibcStat = libc::stat64; pub type OffT = libc::off64_t; pub type StatVfs = libc::statvfs64; @@ -60,7 +65,7 @@ impl InodeHandle { } } - pub fn stat(&self) -> io::Result { + pub fn stat(&self) -> io::Result { match self { InodeHandle::File(f) => stat_fd(f, None), InodeHandle::Handle(_h) => { @@ -192,7 +197,6 @@ impl PassthroughFs { } /// Create a File or File Handle for `name` under directory `dir_fd` to support `lookup()`. - #[cfg(target_os = "linux")] pub fn open_file_and_handle( &self, dir: &impl AsRawFd, @@ -274,7 +278,6 @@ impl PassthroughFs { } } - #[cfg(target_os = "linux")] Opcode::Fallocate => { let op = mode & !(libc::FALLOC_FL_KEEP_SIZE | libc::FALLOC_FL_UNSHARE_RANGE); match op { diff --git a/src/passthrough/passthrough_fs_macos.rs b/src/passthrough/passthrough_fs_macos.rs index 25418f62a..b6335d66e 100644 --- a/src/passthrough/passthrough_fs_macos.rs +++ b/src/passthrough/passthrough_fs_macos.rs @@ -1,4 +1,7 @@ -#![allow(missing_docs)] +// Copyright (C) 2023 Alibaba Cloud. All rights reserved. +// Copyright 2021 Red Hat, Inc. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE-BSD-3-Clause file. use std::{ ffi::{CStr, CString}, @@ -21,7 +24,7 @@ use super::{ pub type InoT = libc::ino_t; pub type InodeMode = u16; -pub type LibCStat = libc::stat; +pub type LibcStat = libc::stat; pub type OffT = libc::off_t; pub type StatVfs = libc::statvfs; diff --git a/src/passthrough/sync_io.rs b/src/passthrough/sync_io.rs index ae4d95104..a15f69212 100644 --- a/src/passthrough/sync_io.rs +++ b/src/passthrough/sync_io.rs @@ -14,6 +14,8 @@ use std::sync::atomic::Ordering; use std::sync::Arc; use std::time::Duration; +#[cfg(target_os = "macos")] +use super::stat::stat as stat_fd; #[cfg(target_os = "linux")] use super::util::stat_fd; use super::*; @@ -59,6 +61,44 @@ impl PassthroughFs { Ok(()) } + pub fn do_getattr( + &self, + inode: Inode, + handle: Option, + ) -> io::Result<(LibcStat, Duration)> { + let st; + let data = self.inode_map.get(inode).map_err(|e| { + error!("fuse: do_getattr ino {} Not find err {:?}", inode, e); + e + })?; + + // kernel sends 0 as handle in case of no_open, and it depends on fuse server to handle + // this case correctly. + if !self.no_open.load(Ordering::Relaxed) && handle.is_some() { + // Safe as we just checked handle + let hd = self.handle_map.get(handle.unwrap(), inode)?; + st = stat_fd( + hd.get_file(), + #[cfg(target_os = "linux")] + None, + ) + } else { + st = data.handle.stat(); + } + + let st = st.map_err(|e| { + error!("fuse: do_getattr stat failed ino {} err {:?}", inode, e); + e + })?; + Ok(( + #[cfg(target_os = "linux")] + st, + #[cfg(target_os = "macos")] + st.st, + self.cfg.attr_timeout, + )) + } + fn do_open( &self, inode: Inode, @@ -622,7 +662,7 @@ impl FileSystem for PassthroughFs { _ctx: &Context, inode: Inode, handle: Option, - ) -> io::Result<(LibCStat, Duration)> { + ) -> io::Result<(LibcStat, Duration)> { self.do_getattr(inode, handle) } @@ -630,10 +670,10 @@ impl FileSystem for PassthroughFs { &self, _ctx: &Context, inode: Inode, - attr: LibCStat, + attr: LibcStat, handle: Option, valid: SetattrValid, - ) -> io::Result<(LibCStat, Duration)> { + ) -> io::Result<(LibcStat, Duration)> { let inode_data = self.inode_map.get(inode)?; enum Data { diff --git a/src/passthrough/sync_io_linux.rs b/src/passthrough/sync_io_linux.rs index bd6d2f168..35c3017ee 100644 --- a/src/passthrough/sync_io_linux.rs +++ b/src/passthrough/sync_io_linux.rs @@ -2,8 +2,6 @@ use std::{ io, mem::{self, size_of}, os::fd::{AsRawFd, RawFd}, - sync::atomic::Ordering, - time::Duration, }; use vm_memory::{bitmap::BitmapSlice, ByteValued}; @@ -13,7 +11,7 @@ use crate::{ passthrough::{os_compat::LinuxDirent64, util::einval}, }; -use super::{util::stat_fd, Handle, Inode, LibCStat, OffT, PassthroughFs}; +use super::{Handle, Inode, OffT, PassthroughFs}; impl PassthroughFs { pub fn do_readdir( @@ -67,7 +65,6 @@ impl PassthroughFs { let mut rem = &buf[..]; let orig_rem_len = rem.len(); - #[cfg(target_os = "linux")] while !rem.is_empty() { // We only use debug asserts here because these values are coming from the kernel and we // trust them implicitly. @@ -137,32 +134,4 @@ impl PassthroughFs { Ok(()) } - - pub fn do_getattr( - &self, - inode: Inode, - handle: Option, - ) -> io::Result<(LibCStat, Duration)> { - let st; - let data = self.inode_map.get(inode).map_err(|e| { - error!("fuse: do_getattr ino {} Not find err {:?}", inode, e); - e - })?; - - // kernel sends 0 as handle in case of no_open, and it depends on fuse server to handle - // this case correctly. - if !self.no_open.load(Ordering::Relaxed) && handle.is_some() { - // Safe as we just checked handle - let hd = self.handle_map.get(handle.unwrap(), inode)?; - st = stat_fd(hd.get_file(), None); - } else { - st = data.handle.stat(); - } - - let st = st.map_err(|e| { - error!("fuse: do_getattr stat failed ino {} err {:?}", inode, e); - e - })?; - Ok((st, self.cfg.attr_timeout)) - } } diff --git a/src/passthrough/sync_io_macos.rs b/src/passthrough/sync_io_macos.rs index 2ef7b18a7..910aecb9e 100644 --- a/src/passthrough/sync_io_macos.rs +++ b/src/passthrough/sync_io_macos.rs @@ -1,8 +1,6 @@ use std::{ io, mem, os::fd::{AsRawFd, RawFd}, - sync::atomic::Ordering, - time::Duration, }; use vm_memory::bitmap::BitmapSlice; @@ -13,7 +11,7 @@ use crate::{ passthrough::util::einval, }; -use super::{stat::stat, Handle, Inode, LibCStat, OffT, PassthroughFs}; +use super::{Handle, Inode, OffT, PassthroughFs}; impl PassthroughFs { pub fn do_readdir( @@ -96,9 +94,6 @@ impl PassthroughFs { add_entry( DirEntry { ino: dirent.d_ino, - #[cfg(target_os = "linux")] - offset: dirent.d_seekoff as u64, - #[cfg(target_os = "macos")] offset: dirent.d_seekoff, type_: dirent.d_type as u32, name, @@ -122,32 +117,4 @@ impl PassthroughFs { Ok(()) } - - pub fn do_getattr( - &self, - inode: Inode, - handle: Option, - ) -> io::Result<(LibCStat, Duration)> { - let st; - let data = self.inode_map.get(inode).map_err(|e| { - error!("fuse: do_getattr ino {} Not find err {:?}", inode, e); - e - })?; - - // kernel sends 0 as handle in case of no_open, and it depends on fuse server to handle - // this case correctly. - if !self.no_open.load(Ordering::Relaxed) && handle.is_some() { - // Safe as we just checked handle - let hd = self.handle_map.get(handle.unwrap(), inode)?; - st = stat(hd.get_file()); - } else { - st = data.handle.stat(); - } - - let st = st.map_err(|e| { - error!("fuse: do_getattr stat failed ino {} err {:?}", inode, e); - e - })?; - Ok((st.st, self.cfg.attr_timeout)) - } }