twizzler_abi/syscall/
object_control.rs

1use twizzler_rt_abi::{
2    error::{ArgumentError, TwzError},
3    Result,
4};
5
6use super::{convert_codes_to_result, twzerr, Syscall};
7use crate::{arch::syscall::raw_syscall, object::ObjID};
8
9bitflags::bitflags! {
10    /// Flags to control operation of the object delete operation.
11    #[derive(Debug, Clone, Copy)]
12    pub struct DeleteFlags : u64 {
13        const FORCE = 1;
14    }
15}
16
17/// Possible object control commands for [sys_object_ctrl].
18#[derive(Clone, Copy, Debug)]
19pub enum ObjectControlCmd {
20    /// Commit an object creation.
21    CreateCommit,
22    /// Delete an object.
23    Delete(DeleteFlags),
24    /// Sync an entire object (non-transactionally)
25    Sync,
26    /// Preload an object's data
27    Preload,
28}
29
30impl From<ObjectControlCmd> for (u64, u64) {
31    fn from(c: ObjectControlCmd) -> Self {
32        match c {
33            ObjectControlCmd::CreateCommit => (0, 0),
34            ObjectControlCmd::Delete(x) => (1, x.bits()),
35            ObjectControlCmd::Sync => (2, 0),
36            ObjectControlCmd::Preload => (3, 0),
37        }
38    }
39}
40
41impl TryFrom<(u64, u64)> for ObjectControlCmd {
42    type Error = TwzError;
43    fn try_from(value: (u64, u64)) -> Result<Self> {
44        Ok(match value.0 {
45            0 => ObjectControlCmd::CreateCommit,
46            1 => ObjectControlCmd::Delete(
47                DeleteFlags::from_bits(value.1).ok_or(ArgumentError::InvalidArgument)?,
48            ),
49            2 => ObjectControlCmd::Sync,
50            3 => ObjectControlCmd::Preload,
51            _ => return Err(ArgumentError::InvalidArgument.into()),
52        })
53    }
54}
55
56/// Perform a kernel operation on this object.
57pub fn sys_object_ctrl(id: ObjID, cmd: ObjectControlCmd) -> Result<()> {
58    let [hi, lo] = id.parts();
59    let (cmd, opts) = cmd.into();
60    let args = [hi, lo, cmd, opts];
61    let (code, val) = unsafe { raw_syscall(Syscall::ObjectCtrl, &args) };
62    convert_codes_to_result(code, val, |c, _| c != 0, |_, _| (), twzerr)
63}