Skip to content

Commit

Permalink
Add mount_setattr
Browse files Browse the repository at this point in the history
  • Loading branch information
yujincheng08 committed Jan 24, 2024
1 parent 6ec74e0 commit 78c5696
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 2 deletions.
29 changes: 29 additions & 0 deletions src/backend/libc/mount/syscalls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,3 +270,32 @@ pub(crate) fn fsconfig_reconfigure(fs_fd: BorrowedFd<'_>) -> io::Result<()> {
))
}
}

#[cfg(linux_kernel)]
#[cfg(not(target_os = "android"))]
#[cfg(feature = "mount")]
pub(crate) fn mount_setattr(
dir_fd: BorrowedFd<'_>,
path: &CStr,
flags: crate::fs::AtFlags,
mount_attr: &crate::mount::MountAttr<'_>,
) -> io::Result<()> {
syscall! {
fn mount_setattr(
dir_fd: c::c_int,
fs_name: *const c::c_char,
flags: c::c_uint,
mount_attr: *const crate::mount::MountAttr<'_>,
size: usize
) via SYS_mount_setattr -> c::c_int
}
unsafe {
ret(mount_setattr(
borrowed_fd(dir_fd),
c_str(path),
flags.bits(),
mount_attr as *const _,
core::mem::size_of::<crate::mount::MountAttr<'_>>(),
))
}
}
14 changes: 13 additions & 1 deletion src/backend/libc/mount/types.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::backend::c;
use crate::fd::BorrowedFd;
use bitflags::bitflags;

#[cfg(linux_kernel)]
Expand Down Expand Up @@ -150,9 +151,10 @@ pub(crate) enum FsConfigCmd {
#[cfg(feature = "mount")]
#[cfg(linux_kernel)]
bitflags! {
/// `MOUNT_ATTR_*` constants for use with [`fsmount`].
/// `MOUNT_ATTR_*` constants for use with [`fsmount`, `mount_setattr`].
///
/// [`fsmount`]: crate::mount::fsmount
/// [`mount_setattr`]: crate::mount::mount_setattr
#[repr(transparent)]
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
pub struct MountAttrFlags: c::c_uint {
Expand Down Expand Up @@ -338,3 +340,13 @@ bitflags! {

#[cfg(linux_kernel)]
pub(crate) struct MountFlagsArg(pub(crate) c::c_ulong);

#[repr(C)]
#[derive(Debug, Copy, Clone)]
#[allow(missing_docs)]
pub struct MountAttr<'a> {
pub attr_set: MountAttrFlags,
pub attr_clr: MountAttrFlags,
pub propagation: MountPropagationFlags,
pub userns_fd: BorrowedFd<'a>,
}
20 changes: 20 additions & 0 deletions src/backend/linux_raw/mount/syscalls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,3 +237,23 @@ pub(crate) fn fsconfig_reconfigure(fs_fd: BorrowedFd<'_>) -> io::Result<()> {
))
}
}

#[cfg(feature = "mount")]
#[inline]
pub(crate) fn mount_setattr(
dir_fd: BorrowedFd<'_>,
path: &CStr,
flags: crate::fs::AtFlags,
mount_attr: &crate::mount::MountAttr<'_>,
) -> io::Result<()> {
unsafe {
ret(syscall_readonly!(
__NR_mount_setattr,
dir_fd,
path,
flags,
mount_attr as *const crate::mount::MountAttr<'_>,
crate::backend::conv::size_of::<crate::mount::MountAttr<'_>, _>()
))
}
}
14 changes: 13 additions & 1 deletion src/backend/linux_raw/mount/types.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::backend::c;
use crate::fd::BorrowedFd;
use bitflags::bitflags;

bitflags! {
Expand Down Expand Up @@ -147,9 +148,10 @@ pub(crate) enum FsConfigCmd {

#[cfg(feature = "mount")]
bitflags! {
/// `MOUNT_ATTR_*` constants for use with [`fsmount`].
/// `MOUNT_ATTR_*` constants for use with [`fsmount`, `mount_setattr`].
///
/// [`fsmount`]: crate::mount::fsmount
/// [`mount_setattr`]: crate::mount::mount_setattr
#[repr(transparent)]
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
pub struct MountAttrFlags: c::c_uint {
Expand Down Expand Up @@ -330,3 +332,13 @@ bitflags! {

#[repr(transparent)]
pub(crate) struct MountFlagsArg(pub(crate) c::c_uint);

#[repr(C)]
#[derive(Debug, Copy, Clone)]
#[allow(missing_docs)]
pub struct MountAttr<'a> {
pub attr_set: MountAttrFlags,
pub attr_clr: MountAttrFlags,
pub propagation: MountPropagationFlags,
pub userns_fd: BorrowedFd<'a>,
}
23 changes: 23 additions & 0 deletions src/mount/misc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//! Miscellaneous mount APIs

use crate::backend::mount::types::MountAttr;
use crate::fd::BorrowedFd;
use crate::fs::AtFlags;
use crate::{backend, io, path};

/// `mount_setattr(dir_fd, path, flags, mount_attr)`
///
/// # References
/// - [Linux]
///
/// [Linux]: https://man7.org/linux/man-pages/man2/mount_setattr.2.html
pub fn mount_setattr<Path: path::Arg>(
dir_fd: BorrowedFd<'_>,
path: Path,
flags: AtFlags,
mount_attr: &MountAttr<'_>,
) -> io::Result<()> {
path.into_with_c_str(|path| {
backend::mount::syscalls::mount_setattr(dir_fd, path, flags, mount_attr)
})
}
2 changes: 2 additions & 0 deletions src/mount/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@

#[cfg(feature = "mount")]
mod fsopen;
mod misc;
mod mount_unmount;
mod types;

#[cfg(feature = "mount")]
pub use fsopen::*;
pub use misc::*;
pub use mount_unmount::*;
pub use types::*;

0 comments on commit 78c5696

Please sign in to comment.