Skip to content
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

transport: refine code syntax, no functional change #161

Merged
merged 1 commit into from
Oct 25, 2023
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
50 changes: 25 additions & 25 deletions src/transport/fusedev/fuse_t_session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use vm_memory::ByteValued;

use super::{
Error::IoError, Error::SessionFailure, FuseBuf, FuseDevWriter, Reader, Result,
FUSE_HEADER_SIZE, FUSE_KERN_BUF_SIZE,
FUSE_HEADER_SIZE, FUSE_KERN_BUF_PAGES,
};
use crate::transport::pagesize;

Expand Down Expand Up @@ -112,23 +112,13 @@ impl FuseSession {
subtype: subtype.to_owned(),
file: None,
file_lock: Arc::new(Mutex::new(())),
bufsize: FUSE_KERN_BUF_SIZE * pagesize() + FUSE_HEADER_SIZE,
bufsize: FUSE_KERN_BUF_PAGES * pagesize() + FUSE_HEADER_SIZE,
monitor_file: None,
wait_handle: None,
readonly,
})
}

/// Mount the fuse mountpoint, building connection with the in kernel fuse driver.
pub fn mount(&mut self) -> Result<()> {
let files = fuse_kern_mount(&self.mountpoint, &self.fsname, &self.subtype, self.readonly)?;
self.file = Some(files.0);
self.monitor_file = Some(files.1);
self.wait_handle = Some(self.send_mount_command()?);

Ok(())
}

/// Expose the associated FUSE session file.
pub fn get_fuse_file(&self) -> Option<&File> {
self.file.as_ref()
Expand All @@ -139,19 +129,6 @@ impl FuseSession {
self.file = Some(file);
}

/// Destroy a fuse session.
pub fn umount(&mut self) -> Result<()> {
if let Some(file) = self.monitor_file.take() {
if self.mountpoint.to_str().is_some() {
fuse_kern_umount(file)
} else {
Err(SessionFailure("invalid mountpoint".to_string()))
}
} else {
Ok(())
}
}

/// Get the mountpoint of the session.
pub fn mountpoint(&self) -> &Path {
&self.mountpoint
Expand All @@ -172,6 +149,29 @@ impl FuseSession {
self.bufsize
}

/// Mount the fuse mountpoint, building connection with the in kernel fuse driver.
pub fn mount(&mut self) -> Result<()> {
let files = fuse_kern_mount(&self.mountpoint, &self.fsname, &self.subtype, self.readonly)?;
self.file = Some(files.0);
self.monitor_file = Some(files.1);
self.wait_handle = Some(self.send_mount_command()?);

Ok(())
}

/// Destroy a fuse session.
pub fn umount(&mut self) -> Result<()> {
if let Some(file) = self.monitor_file.take() {
if self.mountpoint.to_str().is_some() {
fuse_kern_umount(file)
} else {
Err(SessionFailure("invalid mountpoint".to_string()))
}
} else {
Ok(())
}
}

/// Create a new fuse message channel.
pub fn new_channel(&self) -> Result<FuseChannel> {
if let Some(file) = &self.file {
Expand Down
92 changes: 46 additions & 46 deletions src/transport/fusedev/linux_session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
//! sequentially. A FUSE session is a connection from a FUSE mountpoint to a FUSE server daemon.
//! A FUSE session can have multiple FUSE channels so that FUSE requests are handled in parallel.

use mio::{Events, Poll, Token, Waker};
use std::fs::{File, OpenOptions};
use std::ops::Deref;
use std::os::unix::fs::PermissionsExt;
Expand All @@ -17,6 +16,7 @@ use std::os::unix::net::UnixStream;
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex};

use mio::{Events, Poll, Token, Waker};
use nix::errno::Errno;
use nix::fcntl::{fcntl, FcntlArg, FdFlag, OFlag};
use nix::mount::{mount, umount2, MntFlags, MsFlags};
Expand All @@ -27,7 +27,7 @@ use nix::unistd::{getgid, getuid, read};
use super::{
super::pagesize,
Error::{IoError, SessionFailure},
FuseBuf, FuseDevWriter, Reader, Result, FUSE_HEADER_SIZE, FUSE_KERN_BUF_SIZE,
FuseBuf, FuseDevWriter, Reader, Result, FUSE_HEADER_SIZE, FUSE_KERN_BUF_PAGES,
};

// These follows definition from libfuse.
Expand Down Expand Up @@ -90,7 +90,7 @@ impl FuseSession {
subtype: subtype.to_owned(),
file: None,
keep_alive: None,
bufsize: FUSE_KERN_BUF_SIZE * pagesize() + FUSE_HEADER_SIZE,
bufsize: FUSE_KERN_BUF_PAGES * pagesize() + FUSE_HEADER_SIZE,
readonly,
wakers: Mutex::new(Vec::new()),
auto_unmount,
Expand Down Expand Up @@ -123,6 +123,36 @@ impl FuseSession {
self.fusermount.as_str()
}

/// Expose the associated FUSE session file.
pub fn get_fuse_file(&self) -> Option<&File> {
self.file.as_ref()
}

/// Force setting the associated FUSE session file.
pub fn set_fuse_file(&mut self, file: File) {
self.file = Some(file);
}

/// Get the mountpoint of the session.
pub fn mountpoint(&self) -> &Path {
&self.mountpoint
}

/// Get the file system name of the session.
pub fn fsname(&self) -> &str {
&self.fsname
}

/// Get the subtype of the session.
pub fn subtype(&self) -> &str {
&self.subtype
}

/// Get the default buffer size of the session.
pub fn bufsize(&self) -> usize {
self.bufsize
}

/// Mount the fuse mountpoint, building connection with the in kernel fuse driver.
pub fn mount(&mut self) -> Result<()> {
let mut flags = MsFlags::MS_NOSUID | MsFlags::MS_NODEV | MsFlags::MS_NOATIME;
Expand All @@ -148,16 +178,6 @@ impl FuseSession {
Ok(())
}

/// Expose the associated FUSE session file.
pub fn get_fuse_file(&self) -> Option<&File> {
self.file.as_ref()
}

/// Force setting the associated FUSE session file.
pub fn set_fuse_file(&mut self, file: File) {
self.file = Some(file);
}

/// Destroy a fuse session.
pub fn umount(&mut self) -> Result<()> {
// If we have a keep_alive socket, just drop it,
Expand All @@ -173,26 +193,6 @@ impl FuseSession {
}
}

/// Get the mountpoint of the session.
pub fn mountpoint(&self) -> &Path {
&self.mountpoint
}

/// Get the file system name of the session.
pub fn fsname(&self) -> &str {
&self.fsname
}

/// Get the subtype of the session.
pub fn subtype(&self) -> &str {
&self.subtype
}

/// Get the default buffer size of the session.
pub fn bufsize(&self) -> usize {
self.bufsize
}

/// Create a new fuse message channel.
pub fn new_channel(&self) -> Result<FuseChannel> {
if let Some(file) = &self.file {
Expand All @@ -209,15 +209,6 @@ impl FuseSession {
}
}

fn add_waker(&self, waker: Arc<Waker>) -> Result<()> {
let mut wakers = self
.wakers
.lock()
.map_err(|e| SessionFailure(format!("lock wakers: {e}")))?;
wakers.push(waker);
Ok(())
}

/// Wake channel loop and exit
pub fn wake(&self) -> Result<()> {
let wakers = self
Expand All @@ -231,6 +222,15 @@ impl FuseSession {
}
Ok(())
}

fn add_waker(&self, waker: Arc<Waker>) -> Result<()> {
let mut wakers = self
.wakers
.lock()
.map_err(|e| SessionFailure(format!("lock wakers: {e}")))?;
wakers.push(waker);
Ok(())
}
}

impl Drop for FuseSession {
Expand Down Expand Up @@ -442,7 +442,7 @@ fn fuse_kern_mount(
Some(opts.deref()),
) {
Ok(()) => Ok((file, None)),
Err(nix::errno::Errno::EPERM) => fuse_fusermount_mount(
Err(Errno::EPERM) => fuse_fusermount_mount(
mountpoint,
fsname,
subtype,
Expand Down Expand Up @@ -507,7 +507,7 @@ fn fuse_fusermount_mount(
// When its partner recv closes, fusermount will unmount.
// Remove the close-on-exec flag from the socket, so we can pass it to
// fusermount.
nix::fcntl::fcntl(send.as_raw_fd(), FcntlArg::F_SETFD(FdFlag::empty()))
fcntl(send.as_raw_fd(), FcntlArg::F_SETFD(FdFlag::empty()))
.map_err(|e| SessionFailure(format!("Failed to remove close-on-exec flag: {e}")))?;

let mut cmd = match target_mntns {
Expand Down Expand Up @@ -581,14 +581,14 @@ fn fuse_kern_umount(mountpoint: &str, file: File, fusermount: &str) -> Result<()
drop(file);
match umount2(mountpoint, MntFlags::MNT_DETACH) {
Ok(()) => Ok(()),
Err(nix::errno::Errno::EPERM) => fuse_fusermount_umount(mountpoint, fusermount),
Err(Errno::EPERM) => fuse_fusermount_umount(mountpoint, fusermount),
Err(e) => Err(SessionFailure(format!(
"failed to umount {mountpoint}: {e}"
))),
}
}

/// Umount a fuse file system
/// Umount a fuse file system by fusermount helper
fn fuse_fusermount_umount(mountpoint: &str, fusermount: &str) -> Result<()> {
match std::process::Command::new(fusermount)
.arg("--unmount")
Expand Down
67 changes: 33 additions & 34 deletions src/transport/fusedev/macos_session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@
//! sequentially. A FUSE session is a connection from a FUSE mountpoint to a FUSE server daemon.
//! A FUSE session can have multiple FUSE channels so that FUSE requests are handled in parallel.

use core_foundation_sys::base::{CFAllocatorRef, CFIndex, CFRelease};
use core_foundation_sys::string::{kCFStringEncodingUTF8, CFStringCreateWithBytes};
use core_foundation_sys::url::{kCFURLPOSIXPathStyle, CFURLCreateWithFileSystemPath, CFURLRef};
use std::ffi::CString;
use std::fs::File;
use std::io::IoSliceMut;
Expand All @@ -20,6 +17,9 @@ use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicPtr, Ordering};
use std::sync::{Arc, Mutex};

use core_foundation_sys::base::{CFAllocatorRef, CFIndex, CFRelease};
use core_foundation_sys::string::{kCFStringEncodingUTF8, CFStringCreateWithBytes};
use core_foundation_sys::url::{kCFURLPOSIXPathStyle, CFURLCreateWithFileSystemPath, CFURLRef};
use libc::{c_void, proc_pidpath, PROC_PIDPATHINFO_MAXSIZE};
use nix::errno::Errno;
use nix::fcntl::{fcntl, FdFlag, F_SETFD};
Expand All @@ -33,7 +33,7 @@ use nix::{cmsg_space, NixPath};

use super::{
Error::IoError, Error::SessionFailure, FuseBuf, FuseDevWriter, Reader, Result,
FUSE_HEADER_SIZE, FUSE_KERN_BUF_SIZE,
FUSE_HEADER_SIZE, FUSE_KERN_BUF_PAGES,
};
use crate::transport::pagesize;

Expand All @@ -51,9 +51,8 @@ type DADissenterRef = *const __DADissenter;
struct __DASession(c_void);
type DASessionRef = *const __DASession;

type DADiskUnmountCallback = ::std::option::Option<
unsafe extern "C" fn(disk: DADiskRef, dissenter: DADissenterRef, context: *mut c_void),
>;
type DADiskUnmountCallback =
Option<unsafe extern "C" fn(disk: DADiskRef, dissenter: DADissenterRef, context: *mut c_void)>;

extern "C" {
fn DADiskUnmount(
Expand Down Expand Up @@ -113,7 +112,7 @@ impl FuseSession {
fsname: fsname.to_owned(),
subtype: subtype.to_owned(),
file: None,
bufsize: FUSE_KERN_BUF_SIZE * pagesize() + FUSE_HEADER_SIZE,
bufsize: FUSE_KERN_BUF_PAGES * pagesize() + FUSE_HEADER_SIZE,
disk: Mutex::new(None),
dasession: Arc::new(AtomicPtr::new(unsafe {
DASessionCreate(std::ptr::null()) as *mut c_void
Expand All @@ -122,18 +121,6 @@ impl FuseSession {
})
}

/// Mount the fuse mountpoint, building connection with the in kernel fuse driver.
pub fn mount(&mut self) -> Result<()> {
let mut disk = self.disk.lock().expect("lock disk failed");
let file = fuse_kern_mount(&self.mountpoint, &self.fsname, &self.subtype, self.readonly)?;
let session = self.dasession.load(Ordering::SeqCst);
let mount_disk = create_disk(&self.mountpoint, session as DASessionRef);
self.file = Some(file);
*disk = Some(mount_disk);

Ok(())
}

/// Expose the associated FUSE session file.
pub fn get_fuse_file(&self) -> Option<&File> {
self.file.as_ref()
Expand All @@ -144,20 +131,6 @@ impl FuseSession {
self.file = Some(file);
}

/// Destroy a fuse session.
pub fn umount(&mut self) -> Result<()> {
if let Some(file) = self.file.take() {
if self.mountpoint.to_str().is_some() {
let mut disk = self.disk.lock().expect("lock disk failed");
fuse_kern_umount(file, disk.take())
} else {
Err(SessionFailure("invalid mountpoint".to_string()))
}
} else {
Ok(())
}
}

/// Get the mountpoint of the session.
pub fn mountpoint(&self) -> &Path {
&self.mountpoint
Expand All @@ -178,6 +151,32 @@ impl FuseSession {
self.bufsize
}

/// Mount the fuse mountpoint, building connection with the in kernel fuse driver.
pub fn mount(&mut self) -> Result<()> {
let mut disk = self.disk.lock().expect("lock disk failed");
let file = fuse_kern_mount(&self.mountpoint, &self.fsname, &self.subtype, self.readonly)?;
let session = self.dasession.load(Ordering::SeqCst);
let mount_disk = create_disk(&self.mountpoint, session as DASessionRef);
self.file = Some(file);
*disk = Some(mount_disk);

Ok(())
}

/// Destroy a fuse session.
pub fn umount(&mut self) -> Result<()> {
if let Some(file) = self.file.take() {
if self.mountpoint.to_str().is_some() {
let mut disk = self.disk.lock().expect("lock disk failed");
fuse_kern_umount(file, disk.take())
} else {
Err(SessionFailure("invalid mountpoint".to_string()))
}
} else {
Ok(())
}
}

/// Create a new fuse message channel.
pub fn new_channel(&self) -> Result<FuseChannel> {
if let Some(file) = &self.file {
Expand Down
Loading