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
use std::{ io, ptr };
use std::os::unix::io::RawFd;
use crate::sys;


fn execute(fd: RawFd, opcode: libc::c_uint, arg: *const libc::c_void, len: libc::c_uint)
    -> io::Result<()>
{
    unsafe {
        let ret = sys::io_uring_register(fd, opcode, arg, len);
        if ret >= 0 {
            Ok(())
        } else {
            Err(io::Error::last_os_error())
        }
    }
}

/// Register files or user buffers for asynchronous I/O.
#[allow(clippy::module_inception)]
pub mod register {
    use super::*;

    #[non_exhaustive]
    pub enum Target<'a> {
        /// Register buffer,
        /// then use the already registered buffer in
        /// [ReadFixed](crate::opcode::ReadFixed) and [WriteFixed](crate::opcode::WriteFixed).
        Buffers(&'a [libc::iovec]),

        /// Register file descriptor,
        /// then use the already registered fd in [Target::Fixed](crate::opcode::types::Target).
        Files(&'a [RawFd]),

        /// Register eventfd.
        EventFd(RawFd),

        FilesUpdate { offset: u32, fds: &'a [RawFd] },

        /// 5.6 available
        #[cfg(feature = "unstable")]
        EventFdAsync(RawFd),

        /// 5.6 available
        #[cfg(feature = "unstable")]
        Probe(&'a mut Probe),

        /// 5.6 available
        #[cfg(feature = "unstable")]
        Personality
    }

    impl Target<'_> {
        pub(crate) fn execute(&self, fd: RawFd) -> io::Result<()> {
            fn cast_ptr<T>(n: &T) -> *const T {
                n as *const T
            }

            match self {
                Target::Buffers(bufs) =>
                    execute(fd, sys::IORING_REGISTER_BUFFERS, bufs.as_ptr() as *const _, bufs.len() as _),
                Target::Files(fds) =>
                    execute(fd, sys::IORING_REGISTER_FILES, fds.as_ptr() as *const _, fds.len() as _),
                Target::EventFd(eventfd) =>
                    execute(fd, sys::IORING_REGISTER_EVENTFD, cast_ptr::<RawFd>(eventfd) as *const _, 1),
                Target::FilesUpdate { offset, fds } => {
                    let fu = sys::io_uring_files_update {
                        offset: *offset,
                        resv: 0,
                        fds: fds.as_ptr() as _
                    };
                    let fu = &fu as *const sys::io_uring_files_update;

                    execute(fd, sys::IORING_REGISTER_FILES_UPDATE, fu as *const _, fds.len() as _)
                },
                #[cfg(feature = "unstable")]
                Target::EventFdAsync(eventfd) =>
                    execute(fd, sys::IORING_REGISTER_EVENTFD_ASYNC, cast_ptr::<RawFd>(eventfd) as *const _, 1),
                #[cfg(feature = "unstable")]
                Target::Probe(probe) =>
                    execute(fd, sys::IORING_REGISTER_PROBE, probe.0.as_ptr() as *const _, 256),
                #[cfg(feature = "unstable")]
                Target::Personality =>
                    execute(fd, sys::IORING_REGISTER_PERSONALITY, ptr::null(), 0)
            }
        }
    }
}

/// Unregister files or user buffers for asynchronous I/O.
pub mod unregister {
    use super::*;

    #[non_exhaustive]
    pub enum Target {
        /// Unregister buffer.
        Buffers,

        /// Unregister file descriptor.
        Files,

        /// Unregister eventfd.
        EventFd,

        /// 5.6 available
        #[cfg(feature = "unstable")]
        Personality
    }

    impl Target {
        pub(crate) fn execute(&self, fd: RawFd) -> io::Result<()> {
            let opcode = match self {
                Target::Buffers => sys::IORING_UNREGISTER_BUFFERS,
                Target::Files => sys::IORING_UNREGISTER_FILES,
                Target::EventFd => sys::IORING_UNREGISTER_EVENTFD,
                #[cfg(feature = "unstable")]
                Target::Personality => sys::IORING_UNREGISTER_PERSONALITY
            };

            execute(fd, opcode, ptr::null(), 0)
        }
    }
}

#[cfg(feature = "unstable")]
use std::mem;

#[cfg(feature = "unstable")]
pub struct Probe(ptr::NonNull<sys::io_uring_probe>);

#[cfg(feature = "unstable")]
impl Probe {
    const COUNT: usize = 256;
    const SIZE: usize = mem::size_of::<sys::io_uring_probe>()
        + Self::COUNT * mem::size_of::<sys::io_uring_probe_op>();

    #[allow(clippy::cast_ptr_alignment)]
    pub fn new() -> Probe {
        use std::alloc::{ alloc_zeroed, Layout };

        let probe_align = Layout::new::<sys::io_uring_probe>().align();
        let ptr = unsafe {
            let probe_layout = Layout::from_size_align_unchecked(Probe::SIZE, probe_align);
            alloc_zeroed(probe_layout)
        };

        ptr::NonNull::new(ptr)
            .map(ptr::NonNull::cast)
            .map(Probe)
            .expect("Probe alloc failed!")
    }

    pub fn is_supported(&self, opcode: u8) -> bool {
        unsafe {
            let probe = &*self.0.as_ptr();

            if opcode <= probe.last_op {
                let ops = probe.ops.as_slice(Self::COUNT);
                ops[opcode as usize].flags & (sys::IO_URING_OP_SUPPORTED as u16) != 0
            } else {
                false
            }
        }
    }
}

#[cfg(feature = "unstable")]
impl Default for Probe {
    #[inline]
    fn default() -> Probe {
        Probe::new()
    }
}

#[cfg(feature = "unstable")]
impl Drop for Probe {
    fn drop(&mut self) {
        use std::alloc::{ dealloc, Layout };

        let probe_align = Layout::new::<sys::io_uring_probe>().align();
        unsafe {
            let probe_layout = Layout::from_size_align_unchecked(Probe::SIZE, probe_align);
            dealloc(self.0.as_ptr() as *mut _, probe_layout);
        }
    }
}