-
Notifications
You must be signed in to change notification settings - Fork 274
Expand file tree
/
Copy pathioctl.rs
More file actions
236 lines (208 loc) · 7.19 KB
/
Copy pathioctl.rs
File metadata and controls
236 lines (208 loc) · 7.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
//! Filesystem-oriented `ioctl` functions.
#![allow(unsafe_code)]
#[cfg(all(linux_kernel, not(any(target_arch = "sparc", target_arch = "sparc64"))))]
use std::{marker::PhantomData, os::fd::OwnedFd};
#[cfg(linux_kernel)]
use {
crate::backend::c,
crate::fd::AsFd,
crate::{backend, ffi, io, ioctl},
};
use bitflags::bitflags;
#[cfg(all(linux_kernel, not(any(target_arch = "sparc", target_arch = "sparc64"))))]
use crate::fd::{AsRawFd as _, BorrowedFd};
/// `ioctl(fd, BLKSSZGET)`—Returns the logical block size of a block device.
///
/// This is mentioned in the [Linux `openat` manual page].
///
/// [Linux `openat` manual page]: https://man7.org/linux/man-pages/man2/openat.2.html
#[cfg(linux_kernel)]
#[inline]
#[doc(alias = "BLKSSZGET")]
pub fn ioctl_blksszget<Fd: AsFd>(fd: Fd) -> io::Result<u32> {
// SAFETY: `BLZSSZGET` is a getter opcode that gets a `u32`.
unsafe {
let ctl = ioctl::Getter::<{ c::BLKSSZGET }, c::c_uint>::new();
ioctl::ioctl(fd, ctl)
}
}
/// `ioctl(fd, BLKPBSZGET)`—Returns the physical block size of a block device.
#[cfg(linux_kernel)]
#[inline]
#[doc(alias = "BLKPBSZGET")]
pub fn ioctl_blkpbszget<Fd: AsFd>(fd: Fd) -> io::Result<u32> {
// SAFETY: `BLKPBSZGET` is a getter opcode that gets a `u32`.
unsafe {
let ctl = ioctl::Getter::<{ c::BLKPBSZGET }, c::c_uint>::new();
ioctl::ioctl(fd, ctl)
}
}
/// `ioctl(fd, FICLONE, src_fd)`—Share data between open files.
///
/// This ioctl is not available on SPARC platforms.
///
/// # References
/// - [Linux]
///
/// [Linux]: https://man7.org/linux/man-pages/man2/ioctl_ficlone.2.html
#[cfg(all(linux_kernel, not(any(target_arch = "sparc", target_arch = "sparc64"))))]
#[inline]
#[doc(alias = "FICLONE")]
pub fn ioctl_ficlone<Fd: AsFd, SrcFd: AsFd>(fd: Fd, src_fd: SrcFd) -> io::Result<()> {
unsafe { ioctl::ioctl(fd, Ficlone(src_fd.as_fd())) }
}
/// `ioctl(fd, FICLONERANGE, ...)`—share some the data of one file with another file.
///
/// This ioctl is not available on SPARC platforms.
///
/// # References
/// - [Linux]
///
/// [Linux]: https://man7.org/linux/man-pages/man2/ioctl_ficlonerange.2.html
#[cfg(all(linux_kernel, not(any(target_arch = "sparc", target_arch = "sparc64"))))]
#[inline]
#[doc(alias = "FICLONERANGE")]
pub fn ioctl_ficlonerange<Fd: AsFd, SrcFd: AsFd>(
fd: Fd,
src_fd: SrcFd,
src_offset: u64,
src_length: u64,
dest_offset: u64,
) -> io::Result<()> {
unsafe {
ioctl::ioctl(
fd,
Ficlonerange {
src_fd: i64::from(src_fd.as_fd().as_raw_fd()),
src_offset,
src_length,
dest_offset,
_phantom: PhantomData,
},
)
}
}
/// `ioctl(fd, EXT4_IOC_RESIZE_FS, blocks)`—Resize ext4 filesystem on fd.
#[cfg(linux_raw_dep)]
#[inline]
#[doc(alias = "EXT4_IOC_RESIZE_FS")]
pub fn ext4_ioc_resize_fs<Fd: AsFd>(fd: Fd, blocks: u64) -> io::Result<()> {
// SAFETY: `EXT4_IOC_RESIZE_FS` is a pointer setter opcode.
unsafe {
let ctl = ioctl::Setter::<{ backend::fs::EXT4_IOC_RESIZE_FS }, u64>::new(blocks);
ioctl::ioctl(fd, ctl)
}
}
#[cfg(all(linux_kernel, not(any(target_arch = "sparc", target_arch = "sparc64"))))]
struct Ficlone<'a>(BorrowedFd<'a>);
#[cfg(all(linux_kernel, not(any(target_arch = "sparc", target_arch = "sparc64"))))]
unsafe impl ioctl::Ioctl for Ficlone<'_> {
type Output = ();
const IS_MUTATING: bool = false;
fn opcode(&self) -> ioctl::Opcode {
c::FICLONE as ioctl::Opcode
}
fn as_ptr(&mut self) -> *mut c::c_void {
self.0.as_raw_fd() as *mut c::c_void
}
unsafe fn output_from_ptr(
_: ioctl::IoctlOutput,
_: *mut c::c_void,
) -> io::Result<Self::Output> {
Ok(())
}
}
#[cfg(all(linux_kernel, not(any(target_arch = "sparc", target_arch = "sparc64"))))]
#[repr(C)]
struct Ficlonerange<'a> {
src_fd: i64,
src_offset: u64,
src_length: u64,
dest_offset: u64,
_phantom: PhantomData<&'a OwnedFd>,
}
#[cfg(all(linux_kernel, not(any(target_arch = "sparc", target_arch = "sparc64"))))]
unsafe impl ioctl::Ioctl for Ficlonerange<'_> {
type Output = ();
const IS_MUTATING: bool = false;
fn opcode(&self) -> ioctl::Opcode {
c::FICLONERANGE as ioctl::Opcode
}
fn as_ptr(&mut self) -> *mut c::c_void {
std::ptr::from_mut(self) as *mut c::c_void
}
unsafe fn output_from_ptr(
_: ioctl::IoctlOutput,
_: *mut c::c_void,
) -> io::Result<Self::Output> {
Ok(())
}
}
#[cfg(linux_raw_dep)]
bitflags! {
/// `FS_*` constants for use with [`ioctl_getflags`].
///
/// [`ioctl_getflags`]: crate::fs::ioctl::ioctl_getflags
#[repr(transparent)]
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
pub struct IFlags: ffi::c_uint {
/// `FS_APPEND_FL`
const APPEND = linux_raw_sys::general::FS_APPEND_FL;
/// `FS_COMPR_FL`
const COMPRESSED = linux_raw_sys::general::FS_COMPR_FL;
/// `FS_DIRSYNC_FL`
const DIRSYNC = linux_raw_sys::general::FS_DIRSYNC_FL;
/// `FS_IMMUTABLE_FL`
const IMMUTABLE = linux_raw_sys::general::FS_IMMUTABLE_FL;
/// `FS_JOURNAL_DATA_FL`
const JOURNALING = linux_raw_sys::general::FS_JOURNAL_DATA_FL;
/// `FS_NOATIME_FL`
const NOATIME = linux_raw_sys::general::FS_NOATIME_FL;
/// `FS_NOCOW_FL`
const NOCOW = linux_raw_sys::general::FS_NOCOW_FL;
/// `FS_NODUMP_FL`
const NODUMP = linux_raw_sys::general::FS_NODUMP_FL;
/// `FS_NOTAIL_FL`
const NOTAIL = linux_raw_sys::general::FS_NOTAIL_FL;
/// `FS_PROJINHERIT_FL`
const PROJECT_INHERIT = linux_raw_sys::general::FS_PROJINHERIT_FL;
/// `FS_SECRM_FL`
const SECURE_REMOVAL = linux_raw_sys::general::FS_SECRM_FL;
/// `FS_SYNC_FL`
const SYNC = linux_raw_sys::general::FS_SYNC_FL;
/// `FS_TOPDIR_FL`
const TOPDIR = linux_raw_sys::general::FS_TOPDIR_FL;
/// `FS_UNRM_FL`
const UNRM = linux_raw_sys::general::FS_UNRM_FL;
}
}
/// `ioctl(fd, FS_IOC_GETFLAGS)`—Returns the [inode flags] attributes
///
/// [inode flags]: https://man7.org/linux/man-pages/man2/ioctl_iflags.2.html
#[cfg(linux_raw_dep)]
#[inline]
#[doc(alias = "FS_IOC_GETFLAGS")]
pub fn ioctl_getflags<Fd: AsFd>(fd: Fd) -> io::Result<IFlags> {
unsafe {
#[cfg(target_pointer_width = "32")]
let ctl = ioctl::Getter::<{ c::FS_IOC32_GETFLAGS }, u32>::new();
#[cfg(target_pointer_width = "64")]
let ctl = ioctl::Getter::<{ c::FS_IOC_GETFLAGS }, u32>::new();
ioctl::ioctl(fd, ctl).map(IFlags::from_bits_retain)
}
}
/// `ioctl(fd, FS_IOC_SETFLAGS)`—Modify the [inode flags] attributes
///
/// [inode flags]: https://man7.org/linux/man-pages/man2/ioctl_iflags.2.html
#[cfg(linux_raw_dep)]
#[inline]
#[doc(alias = "FS_IOC_SETFLAGS")]
pub fn ioctl_setflags<Fd: AsFd>(fd: Fd, flags: IFlags) -> io::Result<()> {
unsafe {
#[cfg(target_pointer_width = "32")]
let ctl = ioctl::Setter::<{ c::FS_IOC32_SETFLAGS }, u32>::new(flags.bits());
#[cfg(target_pointer_width = "64")]
let ctl = ioctl::Setter::<{ c::FS_IOC_SETFLAGS }, u32>::new(flags.bits());
ioctl::ioctl(fd, ctl)
}
}