Skip to content

Commit 78c5696

Browse files
committed
Add mount_setattr
1 parent 6ec74e0 commit 78c5696

File tree

6 files changed

+100
-2
lines changed

6 files changed

+100
-2
lines changed

src/backend/libc/mount/syscalls.rs

+29
Original file line numberDiff line numberDiff line change
@@ -270,3 +270,32 @@ pub(crate) fn fsconfig_reconfigure(fs_fd: BorrowedFd<'_>) -> io::Result<()> {
270270
))
271271
}
272272
}
273+
274+
#[cfg(linux_kernel)]
275+
#[cfg(not(target_os = "android"))]
276+
#[cfg(feature = "mount")]
277+
pub(crate) fn mount_setattr(
278+
dir_fd: BorrowedFd<'_>,
279+
path: &CStr,
280+
flags: crate::fs::AtFlags,
281+
mount_attr: &crate::mount::MountAttr<'_>,
282+
) -> io::Result<()> {
283+
syscall! {
284+
fn mount_setattr(
285+
dir_fd: c::c_int,
286+
fs_name: *const c::c_char,
287+
flags: c::c_uint,
288+
mount_attr: *const crate::mount::MountAttr<'_>,
289+
size: usize
290+
) via SYS_mount_setattr -> c::c_int
291+
}
292+
unsafe {
293+
ret(mount_setattr(
294+
borrowed_fd(dir_fd),
295+
c_str(path),
296+
flags.bits(),
297+
mount_attr as *const _,
298+
core::mem::size_of::<crate::mount::MountAttr<'_>>(),
299+
))
300+
}
301+
}

src/backend/libc/mount/types.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::backend::c;
2+
use crate::fd::BorrowedFd;
23
use bitflags::bitflags;
34

45
#[cfg(linux_kernel)]
@@ -150,9 +151,10 @@ pub(crate) enum FsConfigCmd {
150151
#[cfg(feature = "mount")]
151152
#[cfg(linux_kernel)]
152153
bitflags! {
153-
/// `MOUNT_ATTR_*` constants for use with [`fsmount`].
154+
/// `MOUNT_ATTR_*` constants for use with [`fsmount`, `mount_setattr`].
154155
///
155156
/// [`fsmount`]: crate::mount::fsmount
157+
/// [`mount_setattr`]: crate::mount::mount_setattr
156158
#[repr(transparent)]
157159
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
158160
pub struct MountAttrFlags: c::c_uint {
@@ -338,3 +340,13 @@ bitflags! {
338340

339341
#[cfg(linux_kernel)]
340342
pub(crate) struct MountFlagsArg(pub(crate) c::c_ulong);
343+
344+
#[repr(C)]
345+
#[derive(Debug, Copy, Clone)]
346+
#[allow(missing_docs)]
347+
pub struct MountAttr<'a> {
348+
pub attr_set: MountAttrFlags,
349+
pub attr_clr: MountAttrFlags,
350+
pub propagation: MountPropagationFlags,
351+
pub userns_fd: BorrowedFd<'a>,
352+
}

src/backend/linux_raw/mount/syscalls.rs

+20
Original file line numberDiff line numberDiff line change
@@ -237,3 +237,23 @@ pub(crate) fn fsconfig_reconfigure(fs_fd: BorrowedFd<'_>) -> io::Result<()> {
237237
))
238238
}
239239
}
240+
241+
#[cfg(feature = "mount")]
242+
#[inline]
243+
pub(crate) fn mount_setattr(
244+
dir_fd: BorrowedFd<'_>,
245+
path: &CStr,
246+
flags: crate::fs::AtFlags,
247+
mount_attr: &crate::mount::MountAttr<'_>,
248+
) -> io::Result<()> {
249+
unsafe {
250+
ret(syscall_readonly!(
251+
__NR_mount_setattr,
252+
dir_fd,
253+
path,
254+
flags,
255+
mount_attr as *const crate::mount::MountAttr<'_>,
256+
crate::backend::conv::size_of::<crate::mount::MountAttr<'_>, _>()
257+
))
258+
}
259+
}

src/backend/linux_raw/mount/types.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::backend::c;
2+
use crate::fd::BorrowedFd;
23
use bitflags::bitflags;
34

45
bitflags! {
@@ -147,9 +148,10 @@ pub(crate) enum FsConfigCmd {
147148

148149
#[cfg(feature = "mount")]
149150
bitflags! {
150-
/// `MOUNT_ATTR_*` constants for use with [`fsmount`].
151+
/// `MOUNT_ATTR_*` constants for use with [`fsmount`, `mount_setattr`].
151152
///
152153
/// [`fsmount`]: crate::mount::fsmount
154+
/// [`mount_setattr`]: crate::mount::mount_setattr
153155
#[repr(transparent)]
154156
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
155157
pub struct MountAttrFlags: c::c_uint {
@@ -330,3 +332,13 @@ bitflags! {
330332

331333
#[repr(transparent)]
332334
pub(crate) struct MountFlagsArg(pub(crate) c::c_uint);
335+
336+
#[repr(C)]
337+
#[derive(Debug, Copy, Clone)]
338+
#[allow(missing_docs)]
339+
pub struct MountAttr<'a> {
340+
pub attr_set: MountAttrFlags,
341+
pub attr_clr: MountAttrFlags,
342+
pub propagation: MountPropagationFlags,
343+
pub userns_fd: BorrowedFd<'a>,
344+
}

src/mount/misc.rs

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//! Miscellaneous mount APIs
2+
3+
use crate::backend::mount::types::MountAttr;
4+
use crate::fd::BorrowedFd;
5+
use crate::fs::AtFlags;
6+
use crate::{backend, io, path};
7+
8+
/// `mount_setattr(dir_fd, path, flags, mount_attr)`
9+
///
10+
/// # References
11+
/// - [Linux]
12+
///
13+
/// [Linux]: https://man7.org/linux/man-pages/man2/mount_setattr.2.html
14+
pub fn mount_setattr<Path: path::Arg>(
15+
dir_fd: BorrowedFd<'_>,
16+
path: Path,
17+
flags: AtFlags,
18+
mount_attr: &MountAttr<'_>,
19+
) -> io::Result<()> {
20+
path.into_with_c_str(|path| {
21+
backend::mount::syscalls::mount_setattr(dir_fd, path, flags, mount_attr)
22+
})
23+
}

src/mount/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@
1010

1111
#[cfg(feature = "mount")]
1212
mod fsopen;
13+
mod misc;
1314
mod mount_unmount;
1415
mod types;
1516

1617
#[cfg(feature = "mount")]
1718
pub use fsopen::*;
19+
pub use misc::*;
1820
pub use mount_unmount::*;
1921
pub use types::*;

0 commit comments

Comments
 (0)