1 use crate::backend::c;
2 use crate::backend::conv::ret;
3 #[cfg(feature = "mount")]
4 use crate::backend::conv::{borrowed_fd, c_str, ret_owned_fd};
5 #[cfg(feature = "mount")]
6 use crate::fd::{BorrowedFd, OwnedFd};
7 use crate::ffi::CStr;
8 use crate::io;
9 use core::ptr::null;
10 
11 #[cfg(linux_kernel)]
mount( source: Option<&CStr>, target: &CStr, file_system_type: Option<&CStr>, flags: super::types::MountFlagsArg, data: Option<&CStr>, ) -> io::Result<()>12 pub(crate) fn mount(
13     source: Option<&CStr>,
14     target: &CStr,
15     file_system_type: Option<&CStr>,
16     flags: super::types::MountFlagsArg,
17     data: Option<&CStr>,
18 ) -> io::Result<()> {
19     unsafe {
20         ret(c::mount(
21             source.map_or_else(null, CStr::as_ptr),
22             target.as_ptr(),
23             file_system_type.map_or_else(null, CStr::as_ptr),
24             flags.0,
25             data.map_or_else(null, CStr::as_ptr).cast(),
26         ))
27     }
28 }
29 
30 #[cfg(linux_kernel)]
unmount(target: &CStr, flags: super::types::UnmountFlags) -> io::Result<()>31 pub(crate) fn unmount(target: &CStr, flags: super::types::UnmountFlags) -> io::Result<()> {
32     unsafe { ret(c::umount2(target.as_ptr(), bitflags_bits!(flags))) }
33 }
34 
35 #[cfg(linux_kernel)]
36 #[cfg(feature = "mount")]
fsopen(fs_name: &CStr, flags: super::types::FsOpenFlags) -> io::Result<OwnedFd>37 pub(crate) fn fsopen(fs_name: &CStr, flags: super::types::FsOpenFlags) -> io::Result<OwnedFd> {
38     syscall! {
39         fn fsopen(
40             fs_name: *const c::c_char,
41             flags: c::c_uint
42         ) via SYS_fsopen -> c::c_int
43     }
44     unsafe { ret_owned_fd(fsopen(c_str(fs_name), flags.bits())) }
45 }
46 
47 #[cfg(linux_kernel)]
48 #[cfg(feature = "mount")]
fsmount( fs_fd: BorrowedFd<'_>, flags: super::types::FsMountFlags, attr_flags: super::types::MountAttrFlags, ) -> io::Result<OwnedFd>49 pub(crate) fn fsmount(
50     fs_fd: BorrowedFd<'_>,
51     flags: super::types::FsMountFlags,
52     attr_flags: super::types::MountAttrFlags,
53 ) -> io::Result<OwnedFd> {
54     syscall! {
55         fn fsmount(
56             fs_fd: c::c_int,
57             flags: c::c_uint,
58             attr_flags: c::c_uint
59         ) via SYS_fsmount -> c::c_int
60     }
61     unsafe { ret_owned_fd(fsmount(borrowed_fd(fs_fd), flags.bits(), attr_flags.bits())) }
62 }
63 
64 #[cfg(linux_kernel)]
65 #[cfg(feature = "mount")]
move_mount( from_dfd: BorrowedFd<'_>, from_pathname: &CStr, to_dfd: BorrowedFd<'_>, to_pathname: &CStr, flags: super::types::MoveMountFlags, ) -> io::Result<()>66 pub(crate) fn move_mount(
67     from_dfd: BorrowedFd<'_>,
68     from_pathname: &CStr,
69     to_dfd: BorrowedFd<'_>,
70     to_pathname: &CStr,
71     flags: super::types::MoveMountFlags,
72 ) -> io::Result<()> {
73     syscall! {
74         fn move_mount(
75             from_dfd: c::c_int,
76             from_pathname: *const c::c_char,
77             to_dfd: c::c_int,
78             to_pathname: *const c::c_char,
79             flags: c::c_uint
80         ) via SYS_move_mount -> c::c_int
81     }
82     unsafe {
83         ret(move_mount(
84             borrowed_fd(from_dfd),
85             c_str(from_pathname),
86             borrowed_fd(to_dfd),
87             c_str(to_pathname),
88             flags.bits(),
89         ))
90     }
91 }
92 
93 #[cfg(linux_kernel)]
94 #[cfg(feature = "mount")]
open_tree( dfd: BorrowedFd<'_>, filename: &CStr, flags: super::types::OpenTreeFlags, ) -> io::Result<OwnedFd>95 pub(crate) fn open_tree(
96     dfd: BorrowedFd<'_>,
97     filename: &CStr,
98     flags: super::types::OpenTreeFlags,
99 ) -> io::Result<OwnedFd> {
100     syscall! {
101         fn open_tree(
102             dfd: c::c_int,
103             filename: *const c::c_char,
104             flags: c::c_uint
105         ) via SYS_open_tree -> c::c_int
106     }
107 
108     unsafe { ret_owned_fd(open_tree(borrowed_fd(dfd), c_str(filename), flags.bits())) }
109 }
110 
111 #[cfg(linux_kernel)]
112 #[cfg(feature = "mount")]
fspick( dfd: BorrowedFd<'_>, path: &CStr, flags: super::types::FsPickFlags, ) -> io::Result<OwnedFd>113 pub(crate) fn fspick(
114     dfd: BorrowedFd<'_>,
115     path: &CStr,
116     flags: super::types::FsPickFlags,
117 ) -> io::Result<OwnedFd> {
118     syscall! {
119         fn fspick(
120             dfd: c::c_int,
121             path: *const c::c_char,
122             flags: c::c_uint
123         ) via SYS_fspick -> c::c_int
124     }
125 
126     unsafe { ret_owned_fd(fspick(borrowed_fd(dfd), c_str(path), flags.bits())) }
127 }
128 
129 #[cfg(feature = "mount")]
130 #[cfg(linux_kernel)]
131 syscall! {
132     fn fsconfig(
133         fs_fd: c::c_int,
134         cmd: c::c_uint,
135         key: *const c::c_char,
136         val: *const c::c_char,
137         aux: c::c_int
138     ) via SYS_fsconfig -> c::c_int
139 }
140 
141 #[cfg(linux_kernel)]
142 #[cfg(feature = "mount")]
fsconfig_set_flag(fs_fd: BorrowedFd<'_>, key: &CStr) -> io::Result<()>143 pub(crate) fn fsconfig_set_flag(fs_fd: BorrowedFd<'_>, key: &CStr) -> io::Result<()> {
144     unsafe {
145         ret(fsconfig(
146             borrowed_fd(fs_fd),
147             super::types::FsConfigCmd::SetFlag as _,
148             c_str(key),
149             null(),
150             0,
151         ))
152     }
153 }
154 
155 #[cfg(linux_kernel)]
156 #[cfg(feature = "mount")]
fsconfig_set_string( fs_fd: BorrowedFd<'_>, key: &CStr, value: &CStr, ) -> io::Result<()>157 pub(crate) fn fsconfig_set_string(
158     fs_fd: BorrowedFd<'_>,
159     key: &CStr,
160     value: &CStr,
161 ) -> io::Result<()> {
162     unsafe {
163         ret(fsconfig(
164             borrowed_fd(fs_fd),
165             super::types::FsConfigCmd::SetString as _,
166             c_str(key),
167             c_str(value),
168             0,
169         ))
170     }
171 }
172 
173 #[cfg(linux_kernel)]
174 #[cfg(feature = "mount")]
fsconfig_set_binary( fs_fd: BorrowedFd<'_>, key: &CStr, value: &[u8], ) -> io::Result<()>175 pub(crate) fn fsconfig_set_binary(
176     fs_fd: BorrowedFd<'_>,
177     key: &CStr,
178     value: &[u8],
179 ) -> io::Result<()> {
180     unsafe {
181         ret(fsconfig(
182             borrowed_fd(fs_fd),
183             super::types::FsConfigCmd::SetBinary as _,
184             c_str(key),
185             value.as_ptr().cast(),
186             value.len().try_into().map_err(|_| io::Errno::OVERFLOW)?,
187         ))
188     }
189 }
190 
191 #[cfg(linux_kernel)]
192 #[cfg(feature = "mount")]
fsconfig_set_fd( fs_fd: BorrowedFd<'_>, key: &CStr, fd: BorrowedFd<'_>, ) -> io::Result<()>193 pub(crate) fn fsconfig_set_fd(
194     fs_fd: BorrowedFd<'_>,
195     key: &CStr,
196     fd: BorrowedFd<'_>,
197 ) -> io::Result<()> {
198     unsafe {
199         ret(fsconfig(
200             borrowed_fd(fs_fd),
201             super::types::FsConfigCmd::SetFd as _,
202             c_str(key),
203             null(),
204             borrowed_fd(fd),
205         ))
206     }
207 }
208 
209 #[cfg(linux_kernel)]
210 #[cfg(feature = "mount")]
fsconfig_set_path( fs_fd: BorrowedFd<'_>, key: &CStr, path: &CStr, fd: BorrowedFd<'_>, ) -> io::Result<()>211 pub(crate) fn fsconfig_set_path(
212     fs_fd: BorrowedFd<'_>,
213     key: &CStr,
214     path: &CStr,
215     fd: BorrowedFd<'_>,
216 ) -> io::Result<()> {
217     unsafe {
218         ret(fsconfig(
219             borrowed_fd(fs_fd),
220             super::types::FsConfigCmd::SetPath as _,
221             c_str(key),
222             c_str(path),
223             borrowed_fd(fd),
224         ))
225     }
226 }
227 
228 #[cfg(linux_kernel)]
229 #[cfg(feature = "mount")]
fsconfig_set_path_empty( fs_fd: BorrowedFd<'_>, key: &CStr, fd: BorrowedFd<'_>, ) -> io::Result<()>230 pub(crate) fn fsconfig_set_path_empty(
231     fs_fd: BorrowedFd<'_>,
232     key: &CStr,
233     fd: BorrowedFd<'_>,
234 ) -> io::Result<()> {
235     unsafe {
236         ret(fsconfig(
237             borrowed_fd(fs_fd),
238             super::types::FsConfigCmd::SetPathEmpty as _,
239             c_str(key),
240             c_str(cstr!("")),
241             borrowed_fd(fd),
242         ))
243     }
244 }
245 
246 #[cfg(linux_kernel)]
247 #[cfg(feature = "mount")]
fsconfig_create(fs_fd: BorrowedFd<'_>) -> io::Result<()>248 pub(crate) fn fsconfig_create(fs_fd: BorrowedFd<'_>) -> io::Result<()> {
249     unsafe {
250         ret(fsconfig(
251             borrowed_fd(fs_fd),
252             super::types::FsConfigCmd::Create as _,
253             null(),
254             null(),
255             0,
256         ))
257     }
258 }
259 
260 #[cfg(linux_kernel)]
261 #[cfg(feature = "mount")]
fsconfig_reconfigure(fs_fd: BorrowedFd<'_>) -> io::Result<()>262 pub(crate) fn fsconfig_reconfigure(fs_fd: BorrowedFd<'_>) -> io::Result<()> {
263     unsafe {
264         ret(fsconfig(
265             borrowed_fd(fs_fd),
266             super::types::FsConfigCmd::Reconfigure as _,
267             null(),
268             null(),
269             0,
270         ))
271     }
272 }
273