Skip to content

Commit 112c268

Browse files
authored
io-uring API updates. (#871)
* io-uring API updates. Add the `addr_len` field to `splice_fd_in_or_file_index_union`, define the `io_uring_sync_cancel_reg` struct, and export the various flags types used in the public API from the `io_uring` module so that users don't need to enable "fs" or "net" themselves just to work with `io_uring`. * Make `Nsecs` be 64-bit on 32-bit x86 Linux.
1 parent 0c22704 commit 112c268

File tree

3 files changed

+73
-28
lines changed

3 files changed

+73
-28
lines changed

src/backend/libc/fs/syscalls.rs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -933,15 +933,23 @@ fn utimensat_old(
933933
.tv_sec
934934
.try_into()
935935
.map_err(|_| io::Errno::OVERFLOW)?,
936-
tv_nsec: times.last_access.tv_nsec,
936+
tv_nsec: times
937+
.last_access
938+
.tv_nsec
939+
.try_into()
940+
.map_err(|_| io::Errno::OVERFLOW)?,
937941
},
938942
c::timespec {
939943
tv_sec: times
940944
.last_modification
941945
.tv_sec
942946
.try_into()
943947
.map_err(|_| io::Errno::OVERFLOW)?,
944-
tv_nsec: times.last_modification.tv_nsec,
948+
tv_nsec: times
949+
.last_modification
950+
.tv_nsec
951+
.try_into()
952+
.map_err(|_| io::Errno::OVERFLOW)?,
945953
},
946954
];
947955
unsafe {
@@ -1515,15 +1523,23 @@ fn futimens_old(fd: BorrowedFd<'_>, times: &Timestamps) -> io::Result<()> {
15151523
.tv_sec
15161524
.try_into()
15171525
.map_err(|_| io::Errno::OVERFLOW)?,
1518-
tv_nsec: times.last_access.tv_nsec,
1526+
tv_nsec: times
1527+
.last_access
1528+
.tv_nsec
1529+
.try_into()
1530+
.map_err(|_| io::Errno::OVERFLOW)?,
15191531
},
15201532
c::timespec {
15211533
tv_sec: times
15221534
.last_modification
15231535
.tv_sec
15241536
.try_into()
15251537
.map_err(|_| io::Errno::OVERFLOW)?,
1526-
tv_nsec: times.last_modification.tv_nsec,
1538+
tv_nsec: times
1539+
.last_modification
1540+
.tv_nsec
1541+
.try_into()
1542+
.map_err(|_| io::Errno::OVERFLOW)?,
15271543
},
15281544
];
15291545

src/io_uring.rs

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ use core::mem::MaybeUninit;
3131
use core::ptr::{null_mut, write_bytes};
3232
use linux_raw_sys::net;
3333

34+
// Export types used in io_uring APIs.
35+
pub use crate::fs::{Advice, AtFlags, OFlags, RenameFlags, ResolveFlags, Statx};
36+
pub use crate::net::{RecvFlags, SendFlags, SocketFlags};
37+
pub use crate::timespec::Timespec;
38+
3439
mod sys {
3540
pub(super) use linux_raw_sys::io_uring::*;
3641
#[cfg(test)]
@@ -1062,20 +1067,20 @@ pub union op_flags_union {
10621067
pub sync_range_flags: u32,
10631068
/// `msg_flags` is split into `send_flags` and `recv_flags`.
10641069
#[doc(alias = "msg_flags")]
1065-
pub send_flags: crate::net::SendFlags,
1070+
pub send_flags: SendFlags,
10661071
/// `msg_flags` is split into `send_flags` and `recv_flags`.
10671072
#[doc(alias = "msg_flags")]
1068-
pub recv_flags: crate::net::RecvFlags,
1073+
pub recv_flags: RecvFlags,
10691074
pub timeout_flags: IoringTimeoutFlags,
1070-
pub accept_flags: crate::net::SocketFlags,
1075+
pub accept_flags: SocketFlags,
10711076
pub cancel_flags: IoringAsyncCancelFlags,
1072-
pub open_flags: crate::fs::OFlags,
1073-
pub statx_flags: crate::fs::AtFlags,
1074-
pub fadvise_advice: crate::fs::Advice,
1077+
pub open_flags: OFlags,
1078+
pub statx_flags: AtFlags,
1079+
pub fadvise_advice: Advice,
10751080
pub splice_flags: SpliceFlags,
1076-
pub rename_flags: crate::fs::RenameFlags,
1077-
pub unlink_flags: crate::fs::AtFlags,
1078-
pub hardlink_flags: crate::fs::AtFlags,
1081+
pub rename_flags: RenameFlags,
1082+
pub unlink_flags: AtFlags,
1083+
pub hardlink_flags: AtFlags,
10791084
pub msg_ring_flags: IoringMsgringFlags,
10801085
}
10811086

@@ -1087,12 +1092,33 @@ pub union buf_union {
10871092
pub buf_group: u16,
10881093
}
10891094

1095+
// TODO: Rename this to include `addr_len` when we have a semver bump?
10901096
#[allow(missing_docs)]
10911097
#[repr(C)]
10921098
#[derive(Copy, Clone)]
10931099
pub union splice_fd_in_or_file_index_union {
10941100
pub splice_fd_in: i32,
10951101
pub file_index: u32,
1102+
pub addr_len: addr_len_struct,
1103+
}
1104+
1105+
#[allow(missing_docs)]
1106+
#[repr(C)]
1107+
#[derive(Copy, Clone)]
1108+
pub struct addr_len_struct {
1109+
pub addr_len: u16,
1110+
pub __pad3: [u16; 1],
1111+
}
1112+
1113+
#[allow(missing_docs)]
1114+
#[repr(C)]
1115+
#[derive(Copy, Clone)]
1116+
pub struct io_uring_sync_cancel_reg {
1117+
pub addr: u64,
1118+
pub fd: i32,
1119+
pub flags: IoringAsyncCancelFlags,
1120+
pub timeout: Timespec,
1121+
pub pad: [u64; 4],
10961122
}
10971123

10981124
/// An io_uring Completion Queue Entry.
@@ -1269,16 +1295,12 @@ pub struct iovec {
12691295
#[derive(Debug, Copy, Clone, Default)]
12701296
pub struct open_how {
12711297
/// An [`OFlags`] value represented as a `u64`.
1272-
///
1273-
/// [`OFlags`]: crate::fs::OFlags
12741298
pub flags: u64,
12751299

12761300
/// A [`Mode`] value represented as a `u64`.
1277-
///
1278-
/// [`Mode`]: crate::fs::Mode
12791301
pub mode: u64,
12801302

1281-
pub resolve: crate::fs::ResolveFlags,
1303+
pub resolve: ResolveFlags,
12821304
}
12831305

12841306
#[allow(missing_docs)]
@@ -1377,6 +1399,7 @@ fn io_uring_layouts() {
13771399
check_renamed_type!(op_flags_union, io_uring_sqe__bindgen_ty_3);
13781400
check_renamed_type!(buf_union, io_uring_sqe__bindgen_ty_4);
13791401
check_renamed_type!(splice_fd_in_or_file_index_union, io_uring_sqe__bindgen_ty_5);
1402+
check_renamed_type!(addr_len_struct, io_uring_sqe__bindgen_ty_5__bindgen_ty_1);
13801403
check_renamed_type!(
13811404
register_or_sqe_op_or_sqe_flags_union,
13821405
io_uring_restriction__bindgen_ty_1
@@ -1460,4 +1483,5 @@ fn io_uring_layouts() {
14601483
check_struct!(open_how, flags, mode, resolve);
14611484
check_struct!(io_uring_buf_reg, ring_addr, ring_entries, bgid, pad, resv);
14621485
check_struct!(io_uring_buf, addr, len, bid, resv);
1486+
check_struct!(io_uring_sync_cancel_reg, addr, fd, flags, timeout, pad);
14631487
}

src/timespec.rs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! `Timespec` and related types, which are used by multiple public API
22
//! modules.
33
4+
#[cfg(not(fix_y2038))]
45
use crate::backend::c;
56

67
/// `struct timespec`
@@ -28,18 +29,22 @@ pub type Secs = c::time_t;
2829
#[cfg(fix_y2038)]
2930
pub type Secs = i64;
3031

31-
/// A type for the `tv_nsec` field of [`Timespec`].
32-
#[cfg(all(libc, target_arch = "x86_64", target_pointer_width = "32"))]
32+
/// A type for the `tv_sec` field of [`Timespec`].
33+
#[cfg(any(
34+
fix_y2038,
35+
linux_raw,
36+
all(libc, target_arch = "x86_64", target_pointer_width = "32")
37+
))]
3338
pub type Nsecs = i64;
3439

3540
/// A type for the `tv_nsec` field of [`Timespec`].
36-
#[cfg(all(libc, not(all(target_arch = "x86_64", target_pointer_width = "32"))))]
41+
#[cfg(all(
42+
not(fix_y2038),
43+
libc,
44+
not(all(target_arch = "x86_64", target_pointer_width = "32"))
45+
))]
3746
pub type Nsecs = c::c_long;
3847

39-
/// A type for the `tv_nsec` field of [`Timespec`].
40-
#[cfg(linux_raw)]
41-
pub type Nsecs = i64;
42-
4348
/// On 32-bit glibc platforms, `timespec` has anonymous padding fields, which
4449
/// Rust doesn't support yet (see `unnamed_fields`), so we define our own
4550
/// struct with explicit padding, with bidirectional `From` impls.
@@ -52,7 +57,7 @@ pub(crate) struct LibcTimespec {
5257
#[cfg(target_endian = "big")]
5358
padding: core::mem::MaybeUninit<u32>,
5459

55-
pub(crate) tv_nsec: Nsecs,
60+
pub(crate) tv_nsec: i32,
5661

5762
#[cfg(target_endian = "little")]
5863
padding: core::mem::MaybeUninit<u32>,
@@ -64,7 +69,7 @@ impl From<LibcTimespec> for Timespec {
6469
fn from(t: LibcTimespec) -> Self {
6570
Self {
6671
tv_sec: t.tv_sec,
67-
tv_nsec: t.tv_nsec,
72+
tv_nsec: t.tv_nsec as _,
6873
}
6974
}
7075
}
@@ -75,7 +80,7 @@ impl From<Timespec> for LibcTimespec {
7580
fn from(t: Timespec) -> Self {
7681
Self {
7782
tv_sec: t.tv_sec,
78-
tv_nsec: t.tv_nsec,
83+
tv_nsec: t.tv_nsec as _,
7984
padding: core::mem::MaybeUninit::uninit(),
8085
}
8186
}

0 commit comments

Comments
 (0)