From 296bd010bdb9cdf0f5d32688a47c535feadd6ef6 Mon Sep 17 00:00:00 2001 From: Jiang Liu Date: Tue, 10 Oct 2023 14:16:06 +0800 Subject: [PATCH] transport: refine code syntax, no functional change Refine code syntax, no functional change. Signed-off-by: Jiang Liu --- src/transport/fusedev/fuse_t_session.rs | 50 +++++++------- src/transport/fusedev/linux_session.rs | 92 ++++++++++++------------- src/transport/fusedev/macos_session.rs | 67 +++++++++--------- src/transport/fusedev/mod.rs | 24 +++---- 4 files changed, 114 insertions(+), 119 deletions(-) diff --git a/src/transport/fusedev/fuse_t_session.rs b/src/transport/fusedev/fuse_t_session.rs index 8509b4ccb..0419b041e 100644 --- a/src/transport/fusedev/fuse_t_session.rs +++ b/src/transport/fusedev/fuse_t_session.rs @@ -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; @@ -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() @@ -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 @@ -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 { if let Some(file) = &self.file { diff --git a/src/transport/fusedev/linux_session.rs b/src/transport/fusedev/linux_session.rs index ddff4cadf..200aa02fd 100644 --- a/src/transport/fusedev/linux_session.rs +++ b/src/transport/fusedev/linux_session.rs @@ -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; @@ -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}; @@ -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. @@ -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, @@ -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; @@ -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, @@ -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 { if let Some(file) = &self.file { @@ -209,15 +209,6 @@ impl FuseSession { } } - fn add_waker(&self, waker: Arc) -> 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 @@ -231,6 +222,15 @@ impl FuseSession { } Ok(()) } + + fn add_waker(&self, waker: Arc) -> Result<()> { + let mut wakers = self + .wakers + .lock() + .map_err(|e| SessionFailure(format!("lock wakers: {e}")))?; + wakers.push(waker); + Ok(()) + } } impl Drop for FuseSession { @@ -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, @@ -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 { @@ -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") diff --git a/src/transport/fusedev/macos_session.rs b/src/transport/fusedev/macos_session.rs index 3a46db8b3..a2206e80c 100644 --- a/src/transport/fusedev/macos_session.rs +++ b/src/transport/fusedev/macos_session.rs @@ -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; @@ -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}; @@ -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; @@ -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; extern "C" { fn DADiskUnmount( @@ -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 @@ -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() @@ -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 @@ -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 { if let Some(file) = &self.file { diff --git a/src/transport/fusedev/mod.rs b/src/transport/fusedev/mod.rs index 0dd1abf38..23d466c1e 100644 --- a/src/transport/fusedev/mod.rs +++ b/src/transport/fusedev/mod.rs @@ -8,7 +8,6 @@ //! buffer and the whole reply message must be written all at once. use std::collections::VecDeque; - use std::io::{self, IoSlice, Write}; use std::marker::PhantomData; use std::mem::ManuallyDrop; @@ -37,8 +36,12 @@ mod fuse_t_session; #[cfg(all(target_os = "macos", feature = "fuse-t"))] pub use fuse_t_session::*; -// These follows definition from libfuse. -pub const FUSE_KERN_BUF_SIZE: usize = 256; +// These follow the definition from libfuse. +/// Maximum size of FUSE message data, 1M with 4K page. +pub const FUSE_KERN_BUF_PAGES: usize = 256; +/// Alias of `FUSE_KERN_BUF_PAGES`. +pub const FUSE_KERN_BUF_SIZE: usize = FUSE_KERN_BUF_PAGES; +/// Maximum size of FUSE message header, 4K. pub const FUSE_HEADER_SIZE: usize = 0x1000; /// A buffer reference wrapper for fuse requests. @@ -287,7 +290,7 @@ impl<'a, S: BitmapSlice> FuseDevWriter<'a, S> { } } -impl<'a, S: BitmapSlice> io::Write for FuseDevWriter<'a, S> { +impl<'a, S: BitmapSlice> Write for FuseDevWriter<'a, S> { fn write(&mut self, data: &[u8]) -> io::Result { self.check_available_space(data.len())?; @@ -381,7 +384,7 @@ mod async_io { self.buf.extend_from_slice(data2); Ok(len) } else { - let bufs = [std::io::IoSlice::new(data), std::io::IoSlice::new(data2)]; + let bufs = [IoSlice::new(data), IoSlice::new(data2)]; writev(self.fd, &bufs) .map(|x| { self.account_written(x); @@ -413,11 +416,7 @@ mod async_io { self.buf.extend_from_slice(data3); Ok(len) } else { - let bufs = [ - std::io::IoSlice::new(data), - std::io::IoSlice::new(data2), - std::io::IoSlice::new(data3), - ]; + let bufs = [IoSlice::new(data), IoSlice::new(data2), IoSlice::new(data3)]; writev(self.fd, &bufs) .map(|x| { self.account_written(x); @@ -499,10 +498,7 @@ mod async_io { io::Error::new(io::ErrorKind::Other, format!("{}", e)) }), (_, _) => { - let bufs = [ - std::io::IoSlice::new(self.buf.as_slice()), - std::io::IoSlice::new(o), - ]; + let bufs = [IoSlice::new(self.buf.as_slice()), IoSlice::new(o)]; writev(self.fd, &bufs).map_err(|e| { error! {"fail to write to fuse device fd {}: {}", self.fd, e}; io::Error::new(io::ErrorKind::Other, format!("{}", e))