Skip to content

Commit

Permalink
fix: fix cr
Browse files Browse the repository at this point in the history
Signed-off-by: akitaSummer <[email protected]>
  • Loading branch information
akitaSummer committed Jan 25, 2024
1 parent fbd70b9 commit 6e88bd8
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 77 deletions.
2 changes: 0 additions & 2 deletions src/passthrough/file_handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
11 changes: 7 additions & 4 deletions src/passthrough/passthrough_fs_linux.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -60,7 +65,7 @@ impl InodeHandle {
}
}

pub fn stat(&self) -> io::Result<LibCStat> {
pub fn stat(&self) -> io::Result<LibcStat> {
match self {
InodeHandle::File(f) => stat_fd(f, None),
InodeHandle::Handle(_h) => {
Expand Down Expand Up @@ -192,7 +197,6 @@ impl<S: BitmapSlice + Send + Sync> PassthroughFs<S> {
}

/// 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,
Expand Down Expand Up @@ -274,7 +278,6 @@ impl<S: BitmapSlice + Send + Sync> PassthroughFs<S> {
}
}

#[cfg(target_os = "linux")]
Opcode::Fallocate => {
let op = mode & !(libc::FALLOC_FL_KEEP_SIZE | libc::FALLOC_FL_UNSHARE_RANGE);
match op {
Expand Down
7 changes: 5 additions & 2 deletions src/passthrough/passthrough_fs_macos.rs
Original file line number Diff line number Diff line change
@@ -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},
Expand All @@ -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;

Expand Down
46 changes: 43 additions & 3 deletions src/passthrough/sync_io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand Down Expand Up @@ -59,6 +61,44 @@ impl<S: BitmapSlice + Send + Sync> PassthroughFs<S> {
Ok(())
}

pub fn do_getattr(
&self,
inode: Inode,
handle: Option<Handle>,
) -> 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,
Expand Down Expand Up @@ -622,18 +662,18 @@ impl<S: BitmapSlice + Send + Sync> FileSystem for PassthroughFs<S> {
_ctx: &Context,
inode: Inode,
handle: Option<Handle>,
) -> io::Result<(LibCStat, Duration)> {
) -> io::Result<(LibcStat, Duration)> {
self.do_getattr(inode, handle)
}

fn setattr(
&self,
_ctx: &Context,
inode: Inode,
attr: LibCStat,
attr: LibcStat,
handle: Option<Handle>,
valid: SetattrValid,
) -> io::Result<(LibCStat, Duration)> {
) -> io::Result<(LibcStat, Duration)> {
let inode_data = self.inode_map.get(inode)?;

enum Data {
Expand Down
33 changes: 1 addition & 32 deletions src/passthrough/sync_io_linux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand All @@ -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<S: BitmapSlice + Send + Sync> PassthroughFs<S> {
pub fn do_readdir(
Expand Down Expand Up @@ -67,7 +65,6 @@ impl<S: BitmapSlice + Send + Sync> PassthroughFs<S> {
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.
Expand Down Expand Up @@ -137,32 +134,4 @@ impl<S: BitmapSlice + Send + Sync> PassthroughFs<S> {

Ok(())
}

pub fn do_getattr(
&self,
inode: Inode,
handle: Option<Handle>,
) -> 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))
}
}
35 changes: 1 addition & 34 deletions src/passthrough/sync_io_macos.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use std::{
io, mem,
os::fd::{AsRawFd, RawFd},
sync::atomic::Ordering,
time::Duration,
};

use vm_memory::bitmap::BitmapSlice;
Expand All @@ -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<S: BitmapSlice + Send + Sync> PassthroughFs<S> {
pub fn do_readdir(
Expand Down Expand Up @@ -96,9 +94,6 @@ impl<S: BitmapSlice + Send + Sync> PassthroughFs<S> {
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,
Expand All @@ -122,32 +117,4 @@ impl<S: BitmapSlice + Send + Sync> PassthroughFs<S> {

Ok(())
}

pub fn do_getattr(
&self,
inode: Inode,
handle: Option<Handle>,
) -> 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))
}
}

0 comments on commit 6e88bd8

Please sign in to comment.