[go: up one dir, main page]

rustix 1.1.2

Safe Rust bindings to POSIX/Unix/Linux/Winsock-like syscalls
Documentation
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
//! Linux [io_uring].
//!
//! This API is very low-level. The main adaptations it makes from the raw
//! Linux io_uring API are the use of appropriately-sized `bitflags`, `enum`,
//! `Result`, `OwnedFd`, `AsFd`, `RawFd`, and `*mut c_void` in place of plain
//! integers.
//!
//! For a higher-level API built on top of this, see the [rustix-uring] crate.
//!
//! # Safety
//!
//! io_uring operates on raw pointers and raw file descriptors. Rustix does not
//! attempt to provide a safe API for these, because the abstraction level is
//! too low for this to be practical. Safety should be introduced in
//! higher-level abstraction layers.
//!
//! # References
//!  - [Linux]
//!  - [io_uring header]
//!
//! [Linux]: https://www.man7.org/linux/man-pages/man7/io_uring.7.html
//! [io_uring]: https://en.wikipedia.org/wiki/Io_uring
//! [io_uring header]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/include/uapi/linux/io_uring.h?h=v6.13
//! [rustix-uring]: https://crates.io/crates/rustix-uring
#![allow(unsafe_code)]

mod bindgen_types;

use crate::fd::{AsFd, BorrowedFd, OwnedFd, RawFd};
use crate::utils::option_as_ptr;
use crate::{backend, io};
use bindgen_types::*;
use core::cmp::Ordering;
use core::ffi::c_void;
use core::hash::{Hash, Hasher};
use core::mem::size_of;
use core::ptr::null_mut;
use linux_raw_sys::net;

// Export types used in io_uring APIs.
pub use crate::clockid::ClockId;
pub use crate::event::epoll::{
    Event as EpollEvent, EventData as EpollEventData, EventFlags as EpollEventFlags,
};
pub use crate::ffi::c_char;
pub use crate::fs::{
    Advice, AtFlags, Mode, OFlags, RenameFlags, ResolveFlags, Statx, StatxFlags, XattrFlags,
};
pub use crate::io::ReadWriteFlags;
pub use crate::kernel_sigset::KernelSigSet;
pub use crate::net::addr::{SocketAddrLen, SocketAddrOpaque, SocketAddrStorage};
pub use crate::net::{RecvFlags, SendFlags, SocketFlags};
pub use crate::signal::Signal;
pub use crate::thread::futex::{
    Wait as FutexWait, WaitFlags as FutexWaitFlags, WaitPtr as FutexWaitPtr,
    WaitvFlags as FutexWaitvFlags,
};
pub use crate::timespec::{Nsecs, Secs, Timespec};

mod sys {
    pub(super) use linux_raw_sys::io_uring::*;
    #[cfg(test)]
    pub(super) use {
        crate::backend::c::iovec, linux_raw_sys::general::open_how, linux_raw_sys::net::msghdr,
    };
}

/// `msghdr`
#[allow(missing_docs)]
#[repr(C)]
pub struct MsgHdr {
    pub msg_name: *mut c_void,
    pub msg_namelen: SocketAddrLen,
    pub msg_iov: *mut iovec,
    pub msg_iovlen: usize,
    pub msg_control: *mut c_void,
    pub msg_controllen: usize,
    pub msg_flags: RecvFlags,
}

/// `io_uring_setup(entries, params)`—Setup a context for performing
/// asynchronous I/O.
///
/// # Safety
///
/// If [`IoringSetupFlags::ATTACH_WQ`] is set, the `wq_fd` field of
/// `io_uring_params` must be an open file descriptor.
///
/// # References
///  - [Linux]
///
/// [Linux]: https://www.man7.org/linux/man-pages/man2/io_uring_setup.2.html
#[inline]
pub unsafe fn io_uring_setup(entries: u32, params: &mut io_uring_params) -> io::Result<OwnedFd> {
    backend::io_uring::syscalls::io_uring_setup(entries, params)
}

/// `io_uring_register(fd, opcode, arg, nr_args)`—Register files or user
/// buffers for asynchronous I/O.
///
/// To pass flags, use [`io_uring_register_with`].
///
/// # Safety
///
/// io_uring operates on raw pointers and raw file descriptors. Users are
/// responsible for ensuring that memory and resources are only accessed in
/// valid ways.
///
/// # References
///  - [Linux]
///
/// [Linux]: https://www.man7.org/linux/man-pages/man2/io_uring_register.2.html
#[inline]
pub unsafe fn io_uring_register<Fd: AsFd>(
    fd: Fd,
    opcode: IoringRegisterOp,
    arg: *const c_void,
    nr_args: u32,
) -> io::Result<u32> {
    backend::io_uring::syscalls::io_uring_register(fd.as_fd(), opcode, arg, nr_args)
}

/// `io_uring_register_with(fd, opcode, flags, arg, nr_args)`—Register files or
/// user buffers for asynchronous I/O.
///
/// # Safety
///
/// io_uring operates on raw pointers and raw file descriptors. Users are
/// responsible for ensuring that memory and resources are only accessed in
/// valid ways.
///
/// # References
///  - [Linux]
///
/// [Linux]: https://www.man7.org/linux/man-pages/man2/io_uring_register.2.html
#[inline]
pub unsafe fn io_uring_register_with<Fd: AsFd>(
    fd: Fd,
    opcode: IoringRegisterOp,
    flags: IoringRegisterFlags,
    arg: *const c_void,
    nr_args: u32,
) -> io::Result<u32> {
    backend::io_uring::syscalls::io_uring_register_with(fd.as_fd(), opcode, flags, arg, nr_args)
}

/// `io_uring_enter(fd, to_submit, min_complete, flags, 0, 0)`—Initiate
/// and/or complete asynchronous I/O.
///
/// This version has no `arg` argument. To pass:
///  - a signal mask, use [`io_uring_enter_sigmask`].
///  - an [`io_uring_getevents_arg`], use [`io_uring_enter_arg`] (aka
///    `io_uring_enter2`).
///
/// # Safety
///
/// io_uring operates on raw pointers and raw file descriptors. Users are
/// responsible for ensuring that memory and resources are only accessed in
/// valid ways.
///
/// And, `flags` must not have [`IoringEnterFlags::EXT_ARG`] or
/// [`IoringEnterFlags::EXT_ARG_REG`] set.
///
/// # References
///  - [Linux]
///
/// [Linux]: https://www.man7.org/linux/man-pages/man2/io_uring_enter.2.html
#[doc(alias = "io_uring_enter2")]
#[inline]
pub unsafe fn io_uring_enter<Fd: AsFd>(
    fd: Fd,
    to_submit: u32,
    min_complete: u32,
    flags: IoringEnterFlags,
) -> io::Result<u32> {
    debug_assert!(!flags.contains(IoringEnterFlags::EXT_ARG));
    debug_assert!(!flags.contains(IoringEnterFlags::EXT_ARG_REG));

    backend::io_uring::syscalls::io_uring_enter(
        fd.as_fd(),
        to_submit,
        min_complete,
        flags,
        null_mut(),
        0,
    )
}

/// `io_uring_enter(fd, to_submit, min_complete, flags, sigmask,
/// sizeof(*sigmask))`— Initiate and/or complete asynchronous I/O, with a
/// signal mask.
///
/// # Safety
///
/// io_uring operates on raw pointers and raw file descriptors. Users are
/// responsible for ensuring that memory and resources are only accessed in
/// valid ways.
///
/// And, `flags` must not have [`IoringEnterFlags::EXT_ARG`] or
/// [`IoringEnterFlags::EXT_ARG_REG`] set.
///
/// And, the `KernelSigSet` referred to by `arg` must not contain any signal
/// numbers reserved by libc.
///
/// # References
///  - [Linux]
///
/// [Linux]: https://www.man7.org/linux/man-pages/man2/io_uring_enter.2.html
#[doc(alias = "io_uring_enter")]
#[inline]
pub unsafe fn io_uring_enter_sigmask<Fd: AsFd>(
    fd: Fd,
    to_submit: u32,
    min_complete: u32,
    flags: IoringEnterFlags,
    sigmask: Option<&KernelSigSet>,
) -> io::Result<u32> {
    debug_assert!(!flags.contains(IoringEnterFlags::EXT_ARG));
    debug_assert!(!flags.contains(IoringEnterFlags::EXT_ARG_REG));

    backend::io_uring::syscalls::io_uring_enter(
        fd.as_fd(),
        to_submit,
        min_complete,
        flags,
        option_as_ptr(sigmask).cast::<c_void>(),
        size_of::<KernelSigSet>(),
    )
}

/// `io_uring_enter2(fd, to_submit, min_complete, flags, arg, sizeof(*arg))`—
/// Initiate and/or complete asynchronous I/O, with a signal mask and a
/// timeout.
///
/// # Safety
///
/// io_uring operates on raw pointers and raw file descriptors. Users are
/// responsible for ensuring that memory and resources are only accessed in
/// valid ways.
///
/// And, `flags` must have [`IoringEnterFlags::EXT_ARG`] set, and must not have
/// [`IoringEnterFlags::EXT_ARG_REG`] set.
///
/// And, the `KernelSigSet` pointed to by the `io_uring_getenvets_arg` referred
/// to by `arg` must not contain any signal numbers reserved by libc.
///
/// # References
///  - [Linux]
///
/// [Linux]: https://www.man7.org/linux/man-pages/man2/io_uring_enter.2.html
#[doc(alias = "io_uring_enter")]
#[doc(alias = "io_uring_enter2")]
#[inline]
pub unsafe fn io_uring_enter_arg<Fd: AsFd>(
    fd: Fd,
    to_submit: u32,
    min_complete: u32,
    flags: IoringEnterFlags,
    arg: Option<&io_uring_getevents_arg>,
) -> io::Result<u32> {
    debug_assert!(flags.contains(IoringEnterFlags::EXT_ARG));
    debug_assert!(!flags.contains(IoringEnterFlags::EXT_ARG_REG));

    backend::io_uring::syscalls::io_uring_enter(
        fd.as_fd(),
        to_submit,
        min_complete,
        flags,
        option_as_ptr(arg).cast::<c_void>(),
        size_of::<io_uring_getevents_arg>(),
    )
}

// TODO: Uncomment this when we support `IoringRegisterOp::CQWAIT_REG`.
/*
/// `io_uring_enter2(fd, to_submit, min_complete, flags, offset,
/// sizeof(io_uring_reg_wait))`— Initiate and/or complete asynchronous I/O,
/// using a previously registered `io_uring_reg_wait`.
///
/// `offset` is an offset into an area of wait regions previously registered
/// with [`io_uring_register`] using the [`IoringRegisterOp::CQWAIT_REG`]
/// operation.
///
/// # Safety
///
/// io_uring operates on raw pointers and raw file descriptors. Users are
/// responsible for ensuring that memory and resources are only accessed in
/// valid ways.
///
/// And, `flags` must have [`IoringEnterFlags::EXT_ARG_REG`] set, and must not
/// have [`IoringEnterFlags::EXT_ARG`] set.
///
/// # References
///  - [Linux]
///
/// [Linux]: https://www.man7.org/linux/man-pages/man2/io_uring_enter.2.html
#[doc(alias = "io_uring_enter")]
#[doc(alias = "io_uring_enter2")]
#[inline]
pub unsafe fn io_uring_enter_reg_wait<Fd: AsFd>(
    fd: Fd,
    to_submit: u32,
    min_complete: u32,
    flags: IoringEnterFlags,
    reg_wait: usize,
) -> io::Result<u32> {
    debug_assert!(!flags.contains(IoringEnterFlags::EXT_ARG));
    debug_assert!(flags.contains(IoringEnterFlags::EXT_ARG_REG));

    backend::io_uring::syscalls::io_uring_enter(
        fd.as_fd(),
        to_submit,
        min_complete,
        flags,
        reg_wait as *mut c_void,
        size_of::<io_uring_reg_wait>(),
    )
}
*/

bitflags::bitflags! {
    /// `IORING_ENTER_*` flags for use with [`io_uring_enter`].
    #[repr(transparent)]
    #[derive(Default, Copy, Clone, Eq, PartialEq, Hash, Debug)]
    pub struct IoringEnterFlags: u32 {
        /// `IORING_ENTER_GETEVENTS`
        const GETEVENTS = sys::IORING_ENTER_GETEVENTS;

        /// `IORING_ENTER_SQ_WAKEUP`
        const SQ_WAKEUP = sys::IORING_ENTER_SQ_WAKEUP;

        /// `IORING_ENTER_SQ_WAIT`
        const SQ_WAIT = sys::IORING_ENTER_SQ_WAIT;

        /// `IORING_ENTER_EXT_ARG` (since Linux 5.11)
        const EXT_ARG = sys::IORING_ENTER_EXT_ARG;

        /// `IORING_ENTER_REGISTERED_RING`
        const REGISTERED_RING = sys::IORING_ENTER_REGISTERED_RING;

        /// `IORING_ENTER_ABS_TIMER` (since Linux 6.12)
        const ABS_TIMER = sys::IORING_ENTER_ABS_TIMER;

        /// `IORING_ENTER_EXT_ARG_REG` (since Linux 6.12)
        const EXT_ARG_REG = sys::IORING_ENTER_EXT_ARG_REG;

        /// <https://docs.rs/bitflags/*/bitflags/#externally-defined-flags>
        const _ = !0;
    }
}

/// `IORING_REGISTER_*` and `IORING_UNREGISTER_*` constants for use with
/// [`io_uring_register`].
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
#[repr(u8)]
#[non_exhaustive]
pub enum IoringRegisterOp {
    /// `IORING_REGISTER_BUFFERS`
    RegisterBuffers = sys::io_uring_register_op::IORING_REGISTER_BUFFERS as _,

    /// `IORING_UNREGISTER_BUFFERS`
    UnregisterBuffers = sys::io_uring_register_op::IORING_UNREGISTER_BUFFERS as _,

    /// `IORING_REGISTER_FILES`
    RegisterFiles = sys::io_uring_register_op::IORING_REGISTER_FILES as _,

    /// `IORING_UNREGISTER_FILES`
    UnregisterFiles = sys::io_uring_register_op::IORING_UNREGISTER_FILES as _,

    /// `IORING_REGISTER_EVENTFD`
    RegisterEventfd = sys::io_uring_register_op::IORING_REGISTER_EVENTFD as _,

    /// `IORING_UNREGISTER_EVENTFD`
    UnregisterEventfd = sys::io_uring_register_op::IORING_UNREGISTER_EVENTFD as _,

    /// `IORING_REGISTER_FILES_UPDATE`
    RegisterFilesUpdate = sys::io_uring_register_op::IORING_REGISTER_FILES_UPDATE as _,

    /// `IORING_REGISTER_EVENTFD_ASYNC`
    RegisterEventfdAsync = sys::io_uring_register_op::IORING_REGISTER_EVENTFD_ASYNC as _,

    /// `IORING_REGISTER_PROBE`
    RegisterProbe = sys::io_uring_register_op::IORING_REGISTER_PROBE as _,

    /// `IORING_REGISTER_PERSONALITY`
    RegisterPersonality = sys::io_uring_register_op::IORING_REGISTER_PERSONALITY as _,

    /// `IORING_UNREGISTER_PERSONALITY`
    UnregisterPersonality = sys::io_uring_register_op::IORING_UNREGISTER_PERSONALITY as _,

    /// `IORING_REGISTER_RESTRICTIONS`
    RegisterRestrictions = sys::io_uring_register_op::IORING_REGISTER_RESTRICTIONS as _,

    /// `IORING_REGISTER_ENABLE_RINGS`
    RegisterEnableRings = sys::io_uring_register_op::IORING_REGISTER_ENABLE_RINGS as _,

    /// `IORING_REGISTER_BUFFERS2`
    RegisterBuffers2 = sys::io_uring_register_op::IORING_REGISTER_BUFFERS2 as _,

    /// `IORING_REGISTER_BUFFERS_UPDATE`
    RegisterBuffersUpdate = sys::io_uring_register_op::IORING_REGISTER_BUFFERS_UPDATE as _,

    /// `IORING_REGISTER_FILES2`
    RegisterFiles2 = sys::io_uring_register_op::IORING_REGISTER_FILES2 as _,

    /// `IORING_REGISTER_FILES_UPDATE2`
    RegisterFilesUpdate2 = sys::io_uring_register_op::IORING_REGISTER_FILES_UPDATE2 as _,

    /// `IORING_REGISTER_IOWQ_AFF`
    RegisterIowqAff = sys::io_uring_register_op::IORING_REGISTER_IOWQ_AFF as _,

    /// `IORING_UNREGISTER_IOWQ_AFF`
    UnregisterIowqAff = sys::io_uring_register_op::IORING_UNREGISTER_IOWQ_AFF as _,

    /// `IORING_REGISTER_IOWQ_MAX_WORKERS`
    RegisterIowqMaxWorkers = sys::io_uring_register_op::IORING_REGISTER_IOWQ_MAX_WORKERS as _,

    /// `IORING_REGISTER_RING_FDS`
    RegisterRingFds = sys::io_uring_register_op::IORING_REGISTER_RING_FDS as _,

    /// `IORING_UNREGISTER_RING_FDS`
    UnregisterRingFds = sys::io_uring_register_op::IORING_UNREGISTER_RING_FDS as _,

    /// `IORING_REGISTER_PBUF_RING`
    RegisterPbufRing = sys::io_uring_register_op::IORING_REGISTER_PBUF_RING as _,

    /// `IORING_UNREGISTER_PBUF_RING`
    UnregisterPbufRing = sys::io_uring_register_op::IORING_UNREGISTER_PBUF_RING as _,

    /// `IORING_REGISTER_SYNC_CANCEL`
    RegisterSyncCancel = sys::io_uring_register_op::IORING_REGISTER_SYNC_CANCEL as _,

    /// `IORING_REGISTER_FILE_ALLOC_RANGE`
    RegisterFileAllocRange = sys::io_uring_register_op::IORING_REGISTER_FILE_ALLOC_RANGE as _,

    /// `IORING_REGISTER_PBUF_STATUS` (since Linux 6.8)
    RegisterPbufStatus = sys::io_uring_register_op::IORING_REGISTER_PBUF_STATUS as _,

    /// `IORING_REGISTER_NAPI` (since Linux 6.9)
    RegisterNapi = sys::io_uring_register_op::IORING_REGISTER_NAPI as _,

    /// `IORING_UNREGISTER_NAPI` (since Linux 6.9)
    UnregisterNapi = sys::io_uring_register_op::IORING_UNREGISTER_NAPI as _,

    /// `IORING_REGISTER_CLOCK` (since Linux 6.12)
    RegisterClock = sys::io_uring_register_op::IORING_REGISTER_CLOCK as _,

    /// `IORING_REGISTER_CLONE_BUFFERS ` (since Linux 6.12)
    RegisterCloneBuffers = sys::io_uring_register_op::IORING_REGISTER_CLONE_BUFFERS as _,

    /// `IORING_REGISTER_SEND_MSG_RING` (since Linux 6.12)
    RegisterSendMsgRing = sys::io_uring_register_op::IORING_REGISTER_SEND_MSG_RING as _,

    /// `IORING_REGISTER_RESIZE_RINGS`(since Linux 6.13)
    RegisterResizeRings = sys::io_uring_register_op::IORING_REGISTER_RESIZE_RINGS as _,
}

bitflags::bitflags! {
    /// `IORING_REGISTER_*` flags for use with [`io_uring_register_with`].
    #[repr(transparent)]
    #[derive(Default, Copy, Clone, Eq, PartialEq, Hash, Debug)]
    pub struct IoringRegisterFlags: u32 {
        /// `IORING_REGISTER_USE_REGISTERED_RING`
        const USE_REGISTERED_RING = sys::io_uring_register_op::IORING_REGISTER_USE_REGISTERED_RING as u32;

        /// <https://docs.rs/bitflags/*/bitflags/#externally-defined-flags>
        const _ = !0;
    }
}

/// `IORING_OP_*` constants for use with [`io_uring_sqe`].
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
#[repr(u8)]
#[non_exhaustive]
pub enum IoringOp {
    /// `IORING_OP_NOP`
    Nop = sys::io_uring_op::IORING_OP_NOP as _,

    /// `IORING_OP_ACCEPT`
    Accept = sys::io_uring_op::IORING_OP_ACCEPT as _,

    /// `IORING_OP_ASYNC_CANCEL`
    AsyncCancel = sys::io_uring_op::IORING_OP_ASYNC_CANCEL as _,

    /// `IORING_OP_CLOSE`
    Close = sys::io_uring_op::IORING_OP_CLOSE as _,

    /// `IORING_OP_CONNECT`
    Connect = sys::io_uring_op::IORING_OP_CONNECT as _,

    /// `IORING_OP_EPOLL_CTL`
    EpollCtl = sys::io_uring_op::IORING_OP_EPOLL_CTL as _,

    /// `IORING_OP_FADVISE`
    Fadvise = sys::io_uring_op::IORING_OP_FADVISE as _,

    /// `IORING_OP_FALLOCATE`
    Fallocate = sys::io_uring_op::IORING_OP_FALLOCATE as _,

    /// `IORING_OP_FILES_UPDATE`
    FilesUpdate = sys::io_uring_op::IORING_OP_FILES_UPDATE as _,

    /// `IORING_OP_FSYNC`
    Fsync = sys::io_uring_op::IORING_OP_FSYNC as _,

    /// `IORING_OP_LINKAT`
    Linkat = sys::io_uring_op::IORING_OP_LINKAT as _,

    /// `IORING_OP_LINK_TIMEOUT`
    LinkTimeout = sys::io_uring_op::IORING_OP_LINK_TIMEOUT as _,

    /// `IORING_OP_MADVISE`
    Madvise = sys::io_uring_op::IORING_OP_MADVISE as _,

    /// `IORING_OP_MKDIRAT`
    Mkdirat = sys::io_uring_op::IORING_OP_MKDIRAT as _,

    /// `IORING_OP_OPENAT`
    Openat = sys::io_uring_op::IORING_OP_OPENAT as _,

    /// `IORING_OP_OPENAT2`
    Openat2 = sys::io_uring_op::IORING_OP_OPENAT2 as _,

    /// `IORING_OP_POLL_ADD`
    PollAdd = sys::io_uring_op::IORING_OP_POLL_ADD as _,

    /// `IORING_OP_POLL_REMOVE`
    PollRemove = sys::io_uring_op::IORING_OP_POLL_REMOVE as _,

    /// `IORING_OP_PROVIDE_BUFFERS`
    ProvideBuffers = sys::io_uring_op::IORING_OP_PROVIDE_BUFFERS as _,

    /// `IORING_OP_READ`
    Read = sys::io_uring_op::IORING_OP_READ as _,

    /// `IORING_OP_READV`
    Readv = sys::io_uring_op::IORING_OP_READV as _,

    /// `IORING_OP_READ_FIXED`
    ReadFixed = sys::io_uring_op::IORING_OP_READ_FIXED as _,

    /// `IORING_OP_RECV`
    Recv = sys::io_uring_op::IORING_OP_RECV as _,

    /// `IORING_OP_RECVMSG`
    Recvmsg = sys::io_uring_op::IORING_OP_RECVMSG as _,

    /// `IORING_OP_REMOVE_BUFFERS`
    RemoveBuffers = sys::io_uring_op::IORING_OP_REMOVE_BUFFERS as _,

    /// `IORING_OP_RENAMEAT`
    Renameat = sys::io_uring_op::IORING_OP_RENAMEAT as _,

    /// `IORING_OP_SEND`
    Send = sys::io_uring_op::IORING_OP_SEND as _,

    /// `IORING_OP_SENDMSG`
    Sendmsg = sys::io_uring_op::IORING_OP_SENDMSG as _,

    /// `IORING_OP_SHUTDOWN`
    Shutdown = sys::io_uring_op::IORING_OP_SHUTDOWN as _,

    /// `IORING_OP_SPLICE`
    Splice = sys::io_uring_op::IORING_OP_SPLICE as _,

    /// `IORING_OP_STATX`
    Statx = sys::io_uring_op::IORING_OP_STATX as _,

    /// `IORING_OP_SYMLINKAT`
    Symlinkat = sys::io_uring_op::IORING_OP_SYMLINKAT as _,

    /// `IORING_OP_SYNC_FILE_RANGE`
    SyncFileRange = sys::io_uring_op::IORING_OP_SYNC_FILE_RANGE as _,

    /// `IORING_OP_TEE`
    Tee = sys::io_uring_op::IORING_OP_TEE as _,

    /// `IORING_OP_TIMEOUT`
    Timeout = sys::io_uring_op::IORING_OP_TIMEOUT as _,

    /// `IORING_OP_TIMEOUT_REMOVE`
    TimeoutRemove = sys::io_uring_op::IORING_OP_TIMEOUT_REMOVE as _,

    /// `IORING_OP_UNLINKAT`
    Unlinkat = sys::io_uring_op::IORING_OP_UNLINKAT as _,

    /// `IORING_OP_WRITE`
    Write = sys::io_uring_op::IORING_OP_WRITE as _,

    /// `IORING_OP_WRITEV`
    Writev = sys::io_uring_op::IORING_OP_WRITEV as _,

    /// `IORING_OP_WRITE_FIXED`
    WriteFixed = sys::io_uring_op::IORING_OP_WRITE_FIXED as _,

    /// `IORING_OP_MSG_RING`
    MsgRing = sys::io_uring_op::IORING_OP_MSG_RING as _,

    /// `IORING_OP_FSETXATTR`
    Fsetxattr = sys::io_uring_op::IORING_OP_FSETXATTR as _,

    /// `IORING_OP_SETXATTR`
    Setxattr = sys::io_uring_op::IORING_OP_SETXATTR as _,

    /// `IORING_OP_FGETXATTR`
    Fgetxattr = sys::io_uring_op::IORING_OP_FGETXATTR as _,

    /// `IORING_OP_GETXATTR`
    Getxattr = sys::io_uring_op::IORING_OP_GETXATTR as _,

    /// `IORING_OP_SOCKET`
    Socket = sys::io_uring_op::IORING_OP_SOCKET as _,

    /// `IORING_OP_URING_CMD`
    UringCmd = sys::io_uring_op::IORING_OP_URING_CMD as _,

    /// `IORING_OP_SEND_ZC`
    SendZc = sys::io_uring_op::IORING_OP_SEND_ZC as _,

    /// `IORING_OP_SENDMSG_ZC`
    SendmsgZc = sys::io_uring_op::IORING_OP_SENDMSG_ZC as _,

    /// `IORING_OP_READ_MULTISHOT` (since Linux 6.7)
    ReadMultishot = sys::io_uring_op::IORING_OP_READ_MULTISHOT as _,

    /// `IORING_OP_WAITID` (since Linux 6.5)
    Waitid = sys::io_uring_op::IORING_OP_WAITID as _,

    /// `IORING_OP_FUTEX_WAIT` (since Linux 6.7)
    FutexWait = sys::io_uring_op::IORING_OP_FUTEX_WAIT as _,

    /// `IORING_OP_FUTEX_WAKE` (since Linux 6.7)
    FutexWake = sys::io_uring_op::IORING_OP_FUTEX_WAKE as _,

    /// `IORING_OP_FUTEX_WAITV` (since Linux 6.7)
    FutexWaitv = sys::io_uring_op::IORING_OP_FUTEX_WAITV as _,

    /// `IORING_OP_FIXED_FD_INSTALL` (since Linux 6.8)
    FixedFdInstall = sys::io_uring_op::IORING_OP_FIXED_FD_INSTALL as _,

    /// `IORING_OP_FTRUNCATE` (since Linux 6.9)
    Ftruncate = sys::io_uring_op::IORING_OP_FTRUNCATE as _,

    /// `IORING_OP_BIND` (since Linux 6.11)
    Bind = sys::io_uring_op::IORING_OP_BIND as _,

    /// `IORING_OP_LISTEN` (since Linux 6.11)
    Listen = sys::io_uring_op::IORING_OP_LISTEN as _,
}

impl Default for IoringOp {
    #[inline]
    fn default() -> Self {
        Self::Nop
    }
}

/// `IORING_RESTRICTION_*` constants for use with [`io_uring_restriction`].
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
#[repr(u16)]
#[non_exhaustive]
pub enum IoringRestrictionOp {
    /// `IORING_RESTRICTION_REGISTER_OP`
    RegisterOp = sys::io_uring_register_restriction_op::IORING_RESTRICTION_REGISTER_OP as _,

    /// `IORING_RESTRICTION_SQE_FLAGS_ALLOWED`
    SqeFlagsAllowed =
        sys::io_uring_register_restriction_op::IORING_RESTRICTION_SQE_FLAGS_ALLOWED as _,

    /// `IORING_RESTRICTION_SQE_FLAGS_REQUIRED`
    SqeFlagsRequired =
        sys::io_uring_register_restriction_op::IORING_RESTRICTION_SQE_FLAGS_REQUIRED as _,

    /// `IORING_RESTRICTION_SQE_OP`
    SqeOp = sys::io_uring_register_restriction_op::IORING_RESTRICTION_SQE_OP as _,
}

impl Default for IoringRestrictionOp {
    #[inline]
    fn default() -> Self {
        Self::RegisterOp
    }
}

/// `IORING_MSG_*` constants which represent commands for use with
/// [`IoringOp::MsgRing`], (`seq.addr`)
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
#[repr(u64)]
#[non_exhaustive]
pub enum IoringMsgringCmds {
    /// `IORING_MSG_DATA`
    Data = sys::io_uring_msg_ring_flags::IORING_MSG_DATA as _,

    /// `IORING_MSG_SEND_FD`
    SendFd = sys::io_uring_msg_ring_flags::IORING_MSG_SEND_FD as _,
}

bitflags::bitflags! {
    /// `IORING_SETUP_*` flags for use with [`io_uring_params`].
    #[repr(transparent)]
    #[derive(Default, Copy, Clone, Eq, PartialEq, Hash, Debug)]
    pub struct IoringSetupFlags: u32 {
        /// `IORING_SETUP_ATTACH_WQ`
        const ATTACH_WQ = sys::IORING_SETUP_ATTACH_WQ;

        /// `IORING_SETUP_CLAMP`
        const CLAMP = sys::IORING_SETUP_CLAMP;

        /// `IORING_SETUP_CQSIZE`
        const CQSIZE = sys::IORING_SETUP_CQSIZE;

        /// `IORING_SETUP_IOPOLL`
        const IOPOLL = sys::IORING_SETUP_IOPOLL;

        /// `IORING_SETUP_R_DISABLED`
        const R_DISABLED = sys::IORING_SETUP_R_DISABLED;

        /// `IORING_SETUP_SQPOLL`
        const SQPOLL = sys::IORING_SETUP_SQPOLL;

        /// `IORING_SETUP_SQ_AFF`
        const SQ_AFF = sys::IORING_SETUP_SQ_AFF;

        /// `IORING_SETUP_SQE128`
        const SQE128 = sys::IORING_SETUP_SQE128;

        /// `IORING_SETUP_CQE32`
        const CQE32 = sys::IORING_SETUP_CQE32;

        /// `IORING_SETUP_SUBMIT_ALL`
        const SUBMIT_ALL = sys::IORING_SETUP_SUBMIT_ALL;

        /// `IORING_SETUP_COOP_TRASKRUN`
        const COOP_TASKRUN = sys::IORING_SETUP_COOP_TASKRUN;

        /// `IORING_SETUP_TASKRUN_FLAG`
        const TASKRUN_FLAG = sys::IORING_SETUP_TASKRUN_FLAG;

        /// `IORING_SETUP_SINGLE_ISSUER`
        const SINGLE_ISSUER = sys::IORING_SETUP_SINGLE_ISSUER;

        /// `IORING_SETUP_DEFER_TASKRUN`
        const DEFER_TASKRUN = sys::IORING_SETUP_DEFER_TASKRUN;

        /// `IORING_SETUP_NO_MMAP`
        const NO_MMAP = sys::IORING_SETUP_NO_MMAP;

        /// `IORING_SETUP_REGISTERED_FD_ONLY`
        const REGISTERED_FD_ONLY = sys::IORING_SETUP_REGISTERED_FD_ONLY;

        /// `IORING_SETUP_NO_SQARRAY`
        const NO_SQARRAY = sys::IORING_SETUP_NO_SQARRAY;

        /// <https://docs.rs/bitflags/*/bitflags/#externally-defined-flags>
        const _ = !0;
    }
}

bitflags::bitflags! {
    /// `IOSQE_*` flags for use with [`io_uring_sqe`].
    #[repr(transparent)]
    #[derive(Default, Copy, Clone, Eq, PartialEq, Hash, Debug)]
    pub struct IoringSqeFlags: u8 {
        /// `1 << IOSQE_ASYNC_BIT`
        const ASYNC = 1 << sys::io_uring_sqe_flags_bit::IOSQE_ASYNC_BIT as u8;

        /// `1 << IOSQE_BUFFER_SELECT_BIT`
        const BUFFER_SELECT = 1 << sys::io_uring_sqe_flags_bit::IOSQE_BUFFER_SELECT_BIT as u8;

        /// `1 << IOSQE_FIXED_FILE_BIT`
        const FIXED_FILE = 1 << sys::io_uring_sqe_flags_bit::IOSQE_FIXED_FILE_BIT as u8;

        /// 1 << `IOSQE_IO_DRAIN_BIT`
        const IO_DRAIN = 1 << sys::io_uring_sqe_flags_bit::IOSQE_IO_DRAIN_BIT as u8;

        /// `1 << IOSQE_IO_HARDLINK_BIT`
        const IO_HARDLINK = 1 << sys::io_uring_sqe_flags_bit::IOSQE_IO_HARDLINK_BIT as u8;

        /// `1 << IOSQE_IO_LINK_BIT`
        const IO_LINK = 1 << sys::io_uring_sqe_flags_bit::IOSQE_IO_LINK_BIT as u8;

        /// `1 << IOSQE_CQE_SKIP_SUCCESS_BIT`
        const CQE_SKIP_SUCCESS = 1 << sys::io_uring_sqe_flags_bit::IOSQE_CQE_SKIP_SUCCESS_BIT as u8;

        /// <https://docs.rs/bitflags/*/bitflags/#externally-defined-flags>
        const _ = !0;
    }
}

bitflags::bitflags! {
    /// `IORING_CQE_F_*` flags for use with [`io_uring_cqe`].
    #[repr(transparent)]
    #[derive(Default, Copy, Clone, Eq, PartialEq, Hash, Debug)]
    pub struct IoringCqeFlags: u32 {
        /// `IORING_CQE_F_BUFFER`
        const BUFFER = bitcast!(sys::IORING_CQE_F_BUFFER);

        /// `IORING_CQE_F_MORE`
        const MORE = bitcast!(sys::IORING_CQE_F_MORE);

        /// `IORING_CQE_F_SOCK_NONEMPTY`
        const SOCK_NONEMPTY = bitcast!(sys::IORING_CQE_F_SOCK_NONEMPTY);

        /// `IORING_CQE_F_NOTIF`
        const NOTIF = bitcast!(sys::IORING_CQE_F_NOTIF);

        /// <https://docs.rs/bitflags/*/bitflags/#externally-defined-flags>
        const _ = !0;
    }
}

bitflags::bitflags! {
    /// `IORING_FSYNC_*` flags for use with [`io_uring_sqe`].
    #[repr(transparent)]
    #[derive(Default, Copy, Clone, Eq, PartialEq, Hash, Debug)]
    pub struct IoringFsyncFlags: u32 {
        /// `IORING_FSYNC_DATASYNC`
        const DATASYNC = sys::IORING_FSYNC_DATASYNC;

        /// <https://docs.rs/bitflags/*/bitflags/#externally-defined-flags>
        const _ = !0;
    }
}

bitflags::bitflags! {
    /// `IORING_TIMEOUT_*` and `IORING_LINK_TIMEOUT_UPDATE` flags for use with
    /// [`io_uring_sqe`].
    #[repr(transparent)]
    #[derive(Default, Copy, Clone, Eq, PartialEq, Hash, Debug)]
    pub struct IoringTimeoutFlags: u32 {
        /// `IORING_TIMEOUT_ABS`
        const ABS = sys::IORING_TIMEOUT_ABS;

        /// `IORING_TIMEOUT_UPDATE`
        const UPDATE = sys::IORING_TIMEOUT_UPDATE;

        /// `IORING_TIMEOUT_BOOTTIME`
        const BOOTTIME = sys::IORING_TIMEOUT_BOOTTIME;

        /// `IORING_TIMEOUT_ETIME_SUCCESS`
        const ETIME_SUCCESS = sys::IORING_TIMEOUT_ETIME_SUCCESS;

        /// `IORING_TIMEOUT_REALTIME`
        const REALTIME = sys::IORING_TIMEOUT_REALTIME;

        /// `IORING_TIMEOUT_CLOCK_MASK`
        const CLOCK_MASK = sys::IORING_TIMEOUT_CLOCK_MASK;

        /// `IORING_TIMEOUT_UPDATE_MASK`
        const UPDATE_MASK = sys::IORING_TIMEOUT_UPDATE_MASK;

        /// `IORING_LINK_TIMEOUT_UPDATE`
        const LINK_TIMEOUT_UPDATE = sys::IORING_LINK_TIMEOUT_UPDATE;

        /// <https://docs.rs/bitflags/*/bitflags/#externally-defined-flags>
        const _ = !0;
    }
}

bitflags::bitflags! {
    /// `SPLICE_F_*` flags for use with [`io_uring_sqe`].
    #[repr(transparent)]
    #[derive(Default, Copy, Clone, Eq, PartialEq, Hash, Debug)]
    pub struct SpliceFlags: u32 {
        /// `SPLICE_F_FD_IN_FIXED`
        const FD_IN_FIXED = sys::SPLICE_F_FD_IN_FIXED;

        /// <https://docs.rs/bitflags/*/bitflags/#externally-defined-flags>
        const _ = !0;
    }
}

bitflags::bitflags! {
    /// `IORING_MSG_RING_*` flags for use with [`io_uring_sqe`].
    #[repr(transparent)]
    #[derive(Default, Copy, Clone, Eq, PartialEq, Hash, Debug)]
    pub struct IoringMsgringFlags: u32 {
        /// `IORING_MSG_RING_CQE_SKIP`
        const CQE_SKIP = sys::IORING_MSG_RING_CQE_SKIP;

        /// `IORING_MSG_RING_FLAGS_PASS`
        const FLAGS_PASS = sys::IORING_MSG_RING_FLAGS_PASS;

        /// <https://docs.rs/bitflags/*/bitflags/#externally-defined-flags>
        const _ = !0;
    }
}

bitflags::bitflags! {
    /// `IORING_URING_CMD_*` flags for use with [`io_uring_sqe`].
    #[repr(transparent)]
    #[derive(Default, Copy, Clone, Eq, PartialEq, Hash, Debug)]
    pub struct IoringUringCmdFlags: u32 {
        /// `IORING_URING_CMD_FIXED`
        const FIXED = sys::IORING_URING_CMD_FIXED;

        /// `IORING_URING_CMD_MASK`
        const MASK = sys::IORING_URING_CMD_MASK;

        /// <https://docs.rs/bitflags/*/bitflags/#externally-defined-flags>
        const _ = !0;
    }
}

bitflags::bitflags! {
    /// `IORING_ASYNC_CANCEL_*` flags for use with [`io_uring_sqe`].
    #[repr(transparent)]
    #[derive(Default, Copy, Clone, Eq, PartialEq, Hash, Debug)]
    pub struct IoringAsyncCancelFlags: u32 {
        /// `IORING_ASYNC_CANCEL_ALL`
        const ALL = sys::IORING_ASYNC_CANCEL_ALL;

        /// `IORING_ASYNC_CANCEL_FD`
        const FD = sys::IORING_ASYNC_CANCEL_FD;

        /// `IORING_ASYNC_CANCEL_FD`
        const ANY = sys::IORING_ASYNC_CANCEL_ANY;

        /// `IORING_ASYNC_CANCEL_FD`
        const FD_FIXED = sys::IORING_ASYNC_CANCEL_FD_FIXED;

        /// <https://docs.rs/bitflags/*/bitflags/#externally-defined-flags>
        const _ = !0;
    }
}

bitflags::bitflags! {
    /// `IORING_FIXED_FD_*` flags for use with [`io_uring_sqe`].
    #[repr(transparent)]
    #[derive(Default, Copy, Clone, Eq, PartialEq, Hash, Debug)]
    pub struct IoringFixedFdFlags: u32 {
        /// `IORING_FIXED_FD_NO_CLOEXEC`
        const NO_CLOEXEC = sys::IORING_FIXED_FD_NO_CLOEXEC;

        /// <https://docs.rs/bitflags/*/bitflags/#externally-defined-flags>
        const _ = !0;
    }
}

bitflags::bitflags! {
    /// `IORING_FEAT_*` flags for use with [`io_uring_params`].
    #[repr(transparent)]
    #[derive(Default, Copy, Clone, Eq, PartialEq, Hash, Debug)]
    pub struct IoringFeatureFlags: u32 {
        /// `IORING_FEAT_CQE_SKIP`
        const CQE_SKIP = sys::IORING_FEAT_CQE_SKIP;

        /// `IORING_FEAT_CUR_PERSONALITY`
        const CUR_PERSONALITY = sys::IORING_FEAT_CUR_PERSONALITY;

        /// `IORING_FEAT_EXT_ARG`
        const EXT_ARG = sys::IORING_FEAT_EXT_ARG;

        /// `IORING_FEAT_FAST_POLL`
        const FAST_POLL = sys::IORING_FEAT_FAST_POLL;

        /// `IORING_FEAT_NATIVE_WORKERS`
        const NATIVE_WORKERS = sys::IORING_FEAT_NATIVE_WORKERS;

        /// `IORING_FEAT_NODROP`
        const NODROP = sys::IORING_FEAT_NODROP;

        /// `IORING_FEAT_POLL_32BITS`
        const POLL_32BITS = sys::IORING_FEAT_POLL_32BITS;

        /// `IORING_FEAT_RSRC_TAGS`
        const RSRC_TAGS = sys::IORING_FEAT_RSRC_TAGS;

        /// `IORING_FEAT_RW_CUR_POS`
        const RW_CUR_POS = sys::IORING_FEAT_RW_CUR_POS;

        /// `IORING_FEAT_SINGLE_MMAP`
        const SINGLE_MMAP = sys::IORING_FEAT_SINGLE_MMAP;

        /// `IORING_FEAT_SQPOLL_NONFIXED`
        const SQPOLL_NONFIXED = sys::IORING_FEAT_SQPOLL_NONFIXED;

        /// `IORING_FEAT_SUBMIT_STABLE`
        const SUBMIT_STABLE = sys::IORING_FEAT_SUBMIT_STABLE;

        /// `IORING_FEAT_LINKED_FILE`
        const LINKED_FILE = sys::IORING_FEAT_LINKED_FILE;

        /// `IORING_FEAT_REG_REG_RING`
        const REG_REG_RING = sys::IORING_FEAT_REG_REG_RING;

        /// `IORING_FEAT_RECVSEND_BUNDLE`
        const RECVSEND_BUNDLE = sys::IORING_FEAT_RECVSEND_BUNDLE;

        /// <https://docs.rs/bitflags/*/bitflags/#externally-defined-flags>
        const _ = !0;
    }
}

bitflags::bitflags! {
    /// `IO_URING_OP_*` flags for use with [`io_uring_probe_op`].
    #[repr(transparent)]
    #[derive(Default, Copy, Clone, Eq, PartialEq, Hash, Debug)]
    pub struct IoringOpFlags: u16 {
        /// `IO_URING_OP_SUPPORTED`
        const SUPPORTED = sys::IO_URING_OP_SUPPORTED as _;

        /// <https://docs.rs/bitflags/*/bitflags/#externally-defined-flags>
        const _ = !0;
    }
}

bitflags::bitflags! {
    /// `IORING_RSRC_*` flags for use with [`io_uring_rsrc_register`].
    #[repr(transparent)]
    #[derive(Default, Copy, Clone, Eq, PartialEq, Hash, Debug)]
    pub struct IoringRsrcFlags: u32 {
        /// `IORING_RSRC_REGISTER_SPARSE`
        const REGISTER_SPARSE = sys::IORING_RSRC_REGISTER_SPARSE as _;

        /// <https://docs.rs/bitflags/*/bitflags/#externally-defined-flags>
        const _ = !0;
    }
}

bitflags::bitflags! {
    /// `IORING_SQ_*` flags.
    #[repr(transparent)]
    #[derive(Default, Copy, Clone, Eq, PartialEq, Hash, Debug)]
    pub struct IoringSqFlags: u32 {
        /// `IORING_SQ_NEED_WAKEUP`
        const NEED_WAKEUP = sys::IORING_SQ_NEED_WAKEUP;

        /// `IORING_SQ_CQ_OVERFLOW`
        const CQ_OVERFLOW = sys::IORING_SQ_CQ_OVERFLOW;

        /// `IORING_SQ_TASKRUN`
        const TASKRUN = sys::IORING_SQ_TASKRUN;

        /// <https://docs.rs/bitflags/*/bitflags/#externally-defined-flags>
        const _ = !0;
    }
}

bitflags::bitflags! {
    /// `IORING_CQ_*` flags.
    #[repr(transparent)]
    #[derive(Default, Copy, Clone, Eq, PartialEq, Hash, Debug)]
    pub struct IoringCqFlags: u32 {
        /// `IORING_CQ_EVENTFD_DISABLED`
        const EVENTFD_DISABLED = sys::IORING_CQ_EVENTFD_DISABLED;

        /// <https://docs.rs/bitflags/*/bitflags/#externally-defined-flags>
        const _ = !0;
    }
}

bitflags::bitflags! {
    /// `IORING_POLL_*` flags.
    #[repr(transparent)]
    #[derive(Default, Copy, Clone, Eq, PartialEq, Hash, Debug)]
    pub struct IoringPollFlags: u32 {
        /// `IORING_POLL_ADD_MULTI`
        const ADD_MULTI = sys::IORING_POLL_ADD_MULTI;

        /// `IORING_POLL_UPDATE_EVENTS`
        const UPDATE_EVENTS = sys::IORING_POLL_UPDATE_EVENTS;

        /// `IORING_POLL_UPDATE_USER_DATA`
        const UPDATE_USER_DATA = sys::IORING_POLL_UPDATE_USER_DATA;

        /// `IORING_POLL_ADD_LEVEL`
        const ADD_LEVEL = sys::IORING_POLL_ADD_LEVEL;

        /// <https://docs.rs/bitflags/*/bitflags/#externally-defined-flags>
        const _ = !0;
    }
}

bitflags::bitflags! {
    /// send/sendmsg flags (`sqe.ioprio`)
    #[repr(transparent)]
    #[derive(Default, Copy, Clone, Eq, PartialEq, Hash, Debug)]
    pub struct IoringSendFlags: u16 {
        /// `IORING_RECVSEND_POLL_FIRST`.
        ///
        /// See also [`IoringRecvFlags::POLL_FIRST`].
        const POLL_FIRST = sys::IORING_RECVSEND_POLL_FIRST as _;

        /// `IORING_RECVSEND_FIXED_BUF`
        ///
        /// See also [`IoringRecvFlags::FIXED_BUF`].
        const FIXED_BUF = sys::IORING_RECVSEND_FIXED_BUF as _;

        /// `IORING_SEND_ZC_REPORT_USAGE` (since Linux 6.2)
        const ZC_REPORT_USAGE = sys::IORING_SEND_ZC_REPORT_USAGE as _;

        /// `IORING_RECVSEND_BUNDLE`
        ///
        /// See also [`IoringRecvFlags::BUNDLE`].
        const BUNDLE = sys::IORING_RECVSEND_BUNDLE as _;

        /// <https://docs.rs/bitflags/*/bitflags/#externally-defined-flags>
        const _ = !0;
    }
}

bitflags::bitflags! {
    /// recv/recvmsg flags (`sqe.ioprio`)
    #[repr(transparent)]
    #[derive(Default, Copy, Clone, Eq, PartialEq, Hash, Debug)]
    pub struct IoringRecvFlags: u16 {
        /// `IORING_RECVSEND_POLL_FIRST`
        ///
        /// See also [`IoringSendFlags::POLL_FIRST`].
        const POLL_FIRST = sys::IORING_RECVSEND_POLL_FIRST as _;

        /// `IORING_RECV_MULTISHOT`
        const MULTISHOT = sys::IORING_RECV_MULTISHOT as _;

        /// `IORING_RECVSEND_FIXED_BUF`
        ///
        /// See also [`IoringSendFlags::FIXED_BUF`].
        const FIXED_BUF = sys::IORING_RECVSEND_FIXED_BUF as _;

        /// `IORING_RECVSEND_BUNDLE`
        ///
        /// See also [`IoringSendFlags::BUNDLE`].
        const BUNDLE = sys::IORING_RECVSEND_BUNDLE as _;

        /// <https://docs.rs/bitflags/*/bitflags/#externally-defined-flags>
        const _ = !0;
    }
}

bitflags::bitflags! {
    /// accept flags (`sqe.ioprio`)
    #[repr(transparent)]
    #[derive(Default, Copy, Clone, Eq, PartialEq, Hash, Debug)]
    pub struct IoringAcceptFlags: u16 {
        /// `IORING_ACCEPT_MULTISHOT`
        const MULTISHOT = sys::IORING_ACCEPT_MULTISHOT as _;

        /// <https://docs.rs/bitflags/*/bitflags/#externally-defined-flags>
        const _ = !0;
    }
}

bitflags::bitflags! {
    /// recvmsg out flags
    #[repr(transparent)]
    #[derive(Default, Copy, Clone, Eq, PartialEq, Hash, Debug)]
    pub struct RecvmsgOutFlags: u32 {
        /// `MSG_EOR`
        const EOR = net::MSG_EOR;

        /// `MSG_TRUNC`
        const TRUNC = net::MSG_TRUNC;

        /// `MSG_CTRUNC`
        const CTRUNC = net::MSG_CTRUNC;

        /// `MSG_OOB`
        const OOB = net::MSG_OOB;

        /// `MSG_ERRQUEUE`
        const ERRQUEUE = net::MSG_ERRQUEUE;

        /// <https://docs.rs/bitflags/*/bitflags/#externally-defined-flags>
        const _ = !0;
    }
}

#[allow(missing_docs)]
pub const IORING_CQE_BUFFER_SHIFT: u32 = sys::IORING_CQE_BUFFER_SHIFT as _;
#[allow(missing_docs)]
pub const IORING_FILE_INDEX_ALLOC: i32 = sys::IORING_FILE_INDEX_ALLOC as _;

// Re-export these as `u64`, which is the `offset` type in `rustix::io::mmap`.
#[allow(missing_docs)]
pub const IORING_OFF_SQ_RING: u64 = sys::IORING_OFF_SQ_RING as _;
#[allow(missing_docs)]
pub const IORING_OFF_CQ_RING: u64 = sys::IORING_OFF_CQ_RING as _;
#[allow(missing_docs)]
pub const IORING_OFF_SQES: u64 = sys::IORING_OFF_SQES as _;

/// `IORING_REGISTER_FILES_SKIP`
// SAFETY: `IORING_REGISTER_FILES_SKIP` is a reserved value that is never
// dynamically allocated, so it'll remain valid for the duration of
// `'static`.
pub const IORING_REGISTER_FILES_SKIP: BorrowedFd<'static> =
    unsafe { BorrowedFd::<'static>::borrow_raw(sys::IORING_REGISTER_FILES_SKIP as RawFd) };

/// `IORING_NOTIF_USAGE_ZC_COPIED` (since Linux 6.2)
pub const IORING_NOTIF_USAGE_ZC_COPIED: i32 = sys::IORING_NOTIF_USAGE_ZC_COPIED as _;

/// A pointer in the io_uring API.
///
/// `io_uring`'s native API represents pointers as `u64` values. In order to
/// preserve strict-provenance, use a `*mut c_void`. On platforms where
/// pointers are narrower than 64 bits, this requires additional padding.
#[repr(C)]
#[cfg_attr(any(target_arch = "arm", target_arch = "powerpc"), repr(align(8)))]
#[derive(Copy, Clone)]
#[non_exhaustive]
pub struct io_uring_ptr {
    #[cfg(all(target_pointer_width = "32", target_endian = "big"))]
    #[doc(hidden)]
    pub __pad32: u32,
    #[cfg(all(target_pointer_width = "16", target_endian = "big"))]
    #[doc(hidden)]
    pub __pad16: u16,

    /// The pointer value.
    pub ptr: *mut c_void,

    #[cfg(all(target_pointer_width = "16", target_endian = "little"))]
    #[doc(hidden)]
    pub __pad16: u16,
    #[cfg(all(target_pointer_width = "32", target_endian = "little"))]
    #[doc(hidden)]
    pub __pad32: u32,
}

impl io_uring_ptr {
    /// Construct a null `io_uring_ptr`.
    #[inline]
    pub const fn null() -> Self {
        Self::new(null_mut())
    }

    /// Construct a new `io_uring_ptr`.
    #[inline]
    pub const fn new(ptr: *mut c_void) -> Self {
        Self {
            ptr,

            #[cfg(target_pointer_width = "16")]
            __pad16: 0,
            #[cfg(any(target_pointer_width = "16", target_pointer_width = "32"))]
            __pad32: 0,
        }
    }
}

impl From<*mut c_void> for io_uring_ptr {
    #[inline]
    fn from(ptr: *mut c_void) -> Self {
        Self::new(ptr)
    }
}

impl PartialEq for io_uring_ptr {
    #[inline]
    fn eq(&self, other: &Self) -> bool {
        self.ptr.eq(&other.ptr)
    }
}

impl Eq for io_uring_ptr {}

#[allow(clippy::non_canonical_partial_ord_impl)]
impl PartialOrd for io_uring_ptr {
    #[inline]
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        self.ptr.partial_cmp(&other.ptr)
    }
}

impl Ord for io_uring_ptr {
    #[inline]
    fn cmp(&self, other: &Self) -> Ordering {
        self.ptr.cmp(&other.ptr)
    }
}

impl Hash for io_uring_ptr {
    #[inline]
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.ptr.hash(state)
    }
}

impl Default for io_uring_ptr {
    #[inline]
    fn default() -> Self {
        Self::null()
    }
}

impl core::fmt::Pointer for io_uring_ptr {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        self.ptr.fmt(f)
    }
}

impl core::fmt::Debug for io_uring_ptr {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        self.ptr.fmt(f)
    }
}

/// User data in the io_uring API.
///
/// `io_uring`'s native API represents `user_data` fields as `u64` values. In
/// order to preserve strict-provenance, use a union which allows users to
/// optionally store pointers.
#[repr(C)]
#[derive(Copy, Clone)]
pub union io_uring_user_data {
    /// An arbitrary `u64`.
    pub u64_: u64,

    /// A pointer.
    pub ptr: io_uring_ptr,
}

impl io_uring_user_data {
    /// Create a zero-initialized `Self`.
    pub const fn zeroed() -> Self {
        // Initialize the `u64_` field, which is the size of the full union.
        // This can use `core::mem::zeroed` in Rust 1.75.
        Self { u64_: 0 }
    }

    /// Return the `u64` value.
    #[inline]
    pub const fn u64_(self) -> u64 {
        // SAFETY: All the fields have the same underlying representation.
        unsafe { self.u64_ }
    }

    /// Create a `Self` from a `u64` value.
    #[inline]
    pub const fn from_u64(u64_: u64) -> Self {
        Self { u64_ }
    }

    /// Return the `ptr` pointer value.
    #[inline]
    pub const fn ptr(self) -> *mut c_void {
        // SAFETY: All the fields have the same underlying representation.
        unsafe { self.ptr }.ptr
    }

    /// Create a `Self` from a pointer value.
    #[inline]
    pub const fn from_ptr(ptr: *mut c_void) -> Self {
        Self {
            ptr: io_uring_ptr::new(ptr),
        }
    }
}

impl From<u64> for io_uring_user_data {
    #[inline]
    fn from(u64_: u64) -> Self {
        Self::from_u64(u64_)
    }
}

impl From<*mut c_void> for io_uring_user_data {
    #[inline]
    fn from(ptr: *mut c_void) -> Self {
        Self::from_ptr(ptr)
    }
}

impl PartialEq for io_uring_user_data {
    #[inline]
    fn eq(&self, other: &Self) -> bool {
        // SAFETY: `io_uring_ptr` and `u64` have the same layout.
        unsafe { self.u64_.eq(&other.u64_) }
    }
}

impl Eq for io_uring_user_data {}

#[allow(clippy::non_canonical_partial_ord_impl)]
impl PartialOrd for io_uring_user_data {
    #[inline]
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        // SAFETY: `io_uring_ptr` and `u64` have the same layout.
        unsafe { self.u64_.partial_cmp(&other.u64_) }
    }
}

impl Ord for io_uring_user_data {
    #[inline]
    fn cmp(&self, other: &Self) -> Ordering {
        // SAFETY: `io_uring_ptr` and `u64` have the same layout.
        unsafe { self.u64_.cmp(&other.u64_) }
    }
}

impl Hash for io_uring_user_data {
    #[inline]
    fn hash<H: Hasher>(&self, state: &mut H) {
        // SAFETY: `io_uring_ptr` and `u64` have the same layout.
        unsafe { self.u64_.hash(state) }
    }
}

impl Default for io_uring_user_data {
    #[inline]
    fn default() -> Self {
        Self::zeroed()
    }
}

impl core::fmt::Debug for io_uring_user_data {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        // SAFETY: Just format as a `u64`, since formatting doesn't preserve
        // provenance, and we don't have a discriminant.
        unsafe { self.u64_.fmt(f) }
    }
}

/// An io_uring Submission Queue Entry.
#[allow(missing_docs)]
#[repr(C)]
#[derive(Copy, Clone, Default)]
pub struct io_uring_sqe {
    pub opcode: IoringOp,
    pub flags: IoringSqeFlags,
    pub ioprio: ioprio_union,
    pub fd: RawFd,
    pub off_or_addr2: off_or_addr2_union,
    pub addr_or_splice_off_in: addr_or_splice_off_in_union,
    pub len: len_union,
    pub op_flags: op_flags_union,
    pub user_data: io_uring_user_data,
    pub buf: buf_union,
    pub personality: u16,
    pub splice_fd_in_or_file_index_or_addr_len: splice_fd_in_or_file_index_or_addr_len_union,
    pub addr3_or_cmd: addr3_or_cmd_union,
}

#[allow(missing_docs)]
#[repr(C)]
#[derive(Copy, Clone)]
pub union ioprio_union {
    pub recv_flags: IoringRecvFlags,
    pub send_flags: IoringSendFlags,
    pub accept_flags: IoringAcceptFlags,
    pub ioprio: u16,
}

#[allow(missing_docs)]
#[repr(C)]
#[derive(Copy, Clone)]
pub union len_union {
    pub poll_flags: IoringPollFlags,
    pub len: u32,
}

#[allow(missing_docs)]
#[repr(C)]
#[derive(Copy, Clone)]
pub union addr3_or_cmd_union {
    pub addr3: addr3_struct,
    pub cmd: [u8; 0],
}

#[allow(missing_docs)]
#[repr(C)]
#[derive(Copy, Clone, Default)]
#[non_exhaustive]
pub struct addr3_struct {
    pub addr3: u64,
    #[doc(hidden)]
    pub __pad2: [u64; 1],
}

#[allow(missing_docs)]
#[repr(C)]
#[derive(Copy, Clone)]
pub union off_or_addr2_union {
    pub off: u64,
    pub addr2: io_uring_ptr,
    pub cmd_op: cmd_op_struct,
    pub user_data: io_uring_user_data,
}

#[allow(missing_docs)]
#[repr(C)]
#[derive(Copy, Clone)]
#[non_exhaustive]
pub struct cmd_op_struct {
    pub cmd_op: u32,
    #[doc(hidden)]
    pub __pad1: u32,
}

#[allow(missing_docs)]
#[repr(C)]
#[derive(Copy, Clone)]
pub union addr_or_splice_off_in_union {
    pub addr: io_uring_ptr,
    pub splice_off_in: u64,
    pub msgring_cmd: IoringMsgringCmds,
    pub user_data: io_uring_user_data,
}

#[allow(missing_docs)]
#[repr(C)]
#[derive(Copy, Clone)]
pub union op_flags_union {
    pub rw_flags: crate::io::ReadWriteFlags,
    pub fsync_flags: IoringFsyncFlags,
    pub poll_events: u16,
    pub poll32_events: u32,
    pub sync_range_flags: u32,
    /// `msg_flags` is split into `send_flags` and `recv_flags`.
    #[doc(alias = "msg_flags")]
    pub send_flags: SendFlags,
    /// `msg_flags` is split into `send_flags` and `recv_flags`.
    #[doc(alias = "msg_flags")]
    pub recv_flags: RecvFlags,
    pub timeout_flags: IoringTimeoutFlags,
    pub accept_flags: SocketFlags,
    pub cancel_flags: IoringAsyncCancelFlags,
    pub open_flags: OFlags,
    pub statx_flags: AtFlags,
    pub fadvise_advice: Advice,
    pub splice_flags: SpliceFlags,
    pub rename_flags: RenameFlags,
    pub unlink_flags: AtFlags,
    pub hardlink_flags: AtFlags,
    pub xattr_flags: XattrFlags,
    pub msg_ring_flags: IoringMsgringFlags,
    pub uring_cmd_flags: IoringUringCmdFlags,
    pub futex_flags: FutexWaitvFlags,
    pub install_fd_flags: IoringFixedFdFlags,
}

#[allow(missing_docs)]
#[repr(C, packed)]
#[derive(Copy, Clone)]
pub union buf_union {
    pub buf_index: u16,
    pub buf_group: u16,
}

#[allow(missing_docs)]
#[repr(C)]
#[derive(Copy, Clone)]
pub union splice_fd_in_or_file_index_or_addr_len_union {
    pub splice_fd_in: i32,
    pub file_index: u32,
    pub addr_len: addr_len_struct,
}

#[allow(missing_docs)]
#[repr(C)]
#[derive(Copy, Clone)]
#[non_exhaustive]
pub struct addr_len_struct {
    pub addr_len: u16,
    #[doc(hidden)]
    pub __pad3: [u16; 1],
}

#[allow(missing_docs)]
#[repr(C)]
#[derive(Copy, Clone)]
#[non_exhaustive]
pub struct io_uring_sync_cancel_reg {
    pub addr: io_uring_user_data,
    pub fd: i32,
    pub flags: IoringAsyncCancelFlags,
    pub timeout: Timespec,
    pub opcode: u8,
    #[doc(hidden)]
    pub pad: [u8; 7],
    #[doc(hidden)]
    pub pad2: [u64; 3],
}

impl Default for io_uring_sync_cancel_reg {
    #[inline]
    fn default() -> Self {
        Self {
            addr: Default::default(),
            fd: Default::default(),
            flags: Default::default(),
            timeout: Timespec {
                tv_sec: 0,
                tv_nsec: 0,
            },
            opcode: Default::default(),
            pad: Default::default(),
            pad2: Default::default(),
        }
    }
}

/// An io_uring Completion Queue Entry.
///
/// This does not derive `Copy` or `Clone` because the `big_cqe` field is not
/// automatically copyable.
#[allow(missing_docs)]
#[repr(C)]
#[derive(Debug, Default)]
pub struct io_uring_cqe {
    pub user_data: io_uring_user_data,
    pub res: i32,
    pub flags: IoringCqeFlags,
    pub big_cqe: IncompleteArrayField<u64>,
}

#[allow(missing_docs)]
#[repr(C)]
#[derive(Copy, Clone, Default)]
#[non_exhaustive]
pub struct io_uring_restriction {
    pub opcode: IoringRestrictionOp,
    pub register_or_sqe_op_or_sqe_flags: register_or_sqe_op_or_sqe_flags_union,
    #[doc(hidden)]
    pub resv: u8,
    #[doc(hidden)]
    pub resv2: [u32; 3],
}

#[allow(missing_docs)]
#[repr(C)]
#[derive(Copy, Clone)]
pub union register_or_sqe_op_or_sqe_flags_union {
    pub register_op: IoringRegisterOp,
    pub sqe_op: IoringOp,
    pub sqe_flags: IoringSqeFlags,
}

#[allow(missing_docs)]
#[repr(C)]
#[derive(Debug, Copy, Clone, Default)]
#[non_exhaustive]
pub struct io_uring_params {
    pub sq_entries: u32,
    pub cq_entries: u32,
    pub flags: IoringSetupFlags,
    pub sq_thread_cpu: u32,
    pub sq_thread_idle: u32,
    pub features: IoringFeatureFlags,
    pub wq_fd: RawFd,
    #[doc(hidden)]
    pub resv: [u32; 3],
    pub sq_off: io_sqring_offsets,
    pub cq_off: io_cqring_offsets,
}

#[allow(missing_docs)]
#[repr(C)]
#[derive(Debug, Copy, Clone, Default)]
#[non_exhaustive]
pub struct io_sqring_offsets {
    pub head: u32,
    pub tail: u32,
    pub ring_mask: u32,
    pub ring_entries: u32,
    pub flags: u32,
    pub dropped: u32,
    pub array: u32,
    #[doc(hidden)]
    pub resv1: u32,
    pub user_addr: io_uring_ptr,
}

#[allow(missing_docs)]
#[repr(C)]
#[derive(Debug, Copy, Clone, Default)]
#[non_exhaustive]
pub struct io_cqring_offsets {
    pub head: u32,
    pub tail: u32,
    pub ring_mask: u32,
    pub ring_entries: u32,
    pub overflow: u32,
    pub cqes: u32,
    pub flags: u32,
    #[doc(hidden)]
    pub resv1: u32,
    pub user_addr: io_uring_ptr,
}

#[allow(missing_docs)]
#[repr(C)]
#[derive(Debug, Default)]
#[non_exhaustive]
pub struct io_uring_probe {
    pub last_op: IoringOp,
    pub ops_len: u8,
    #[doc(hidden)]
    pub resv: u16,
    #[doc(hidden)]
    pub resv2: [u32; 3],
    pub ops: IncompleteArrayField<io_uring_probe_op>,
}

#[allow(missing_docs)]
#[repr(C)]
#[derive(Debug, Copy, Clone, Default)]
#[non_exhaustive]
pub struct io_uring_probe_op {
    pub op: IoringOp,
    #[doc(hidden)]
    pub resv: u8,
    pub flags: IoringOpFlags,
    #[doc(hidden)]
    pub resv2: u32,
}

#[allow(missing_docs)]
#[repr(C, align(8))]
#[derive(Debug, Copy, Clone, Default)]
#[non_exhaustive]
pub struct io_uring_files_update {
    pub offset: u32,
    #[doc(hidden)]
    pub resv: u32,
    pub fds: io_uring_ptr,
}

#[allow(missing_docs)]
#[repr(C, align(8))]
#[derive(Debug, Copy, Clone, Default)]
#[non_exhaustive]
pub struct io_uring_rsrc_register {
    pub nr: u32,
    pub flags: IoringRsrcFlags,
    #[doc(hidden)]
    pub resv2: u64,
    pub data: io_uring_ptr,
    pub tags: io_uring_ptr,
}

#[allow(missing_docs)]
#[repr(C, align(8))]
#[derive(Debug, Copy, Clone, Default)]
#[non_exhaustive]
pub struct io_uring_rsrc_update {
    pub offset: u32,
    #[doc(hidden)]
    pub resv: u32,
    pub data: io_uring_ptr,
}

#[allow(missing_docs)]
#[repr(C, align(8))]
#[derive(Debug, Copy, Clone, Default)]
#[non_exhaustive]
pub struct io_uring_rsrc_update2 {
    pub offset: u32,
    #[doc(hidden)]
    pub resv: u32,
    pub data: io_uring_ptr,
    pub tags: io_uring_ptr,
    pub nr: u32,
    #[doc(hidden)]
    pub resv2: u32,
}

#[allow(missing_docs)]
#[repr(C)]
#[derive(Debug, Copy, Clone, Default)]
pub struct io_uring_getevents_arg {
    pub sigmask: io_uring_ptr,
    pub sigmask_sz: u32,
    pub min_wait_usec: u32,
    pub ts: io_uring_ptr,
}

#[allow(missing_docs)]
#[repr(C)]
#[derive(Debug, Default, Copy, Clone)]
pub struct io_uring_recvmsg_out {
    pub namelen: SocketAddrLen,
    pub controllen: u32,
    pub payloadlen: u32,
    pub flags: RecvmsgOutFlags,
}

#[allow(missing_docs)]
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct iovec {
    pub iov_base: *mut c_void,
    pub iov_len: usize,
}

#[allow(missing_docs)]
#[repr(C)]
#[derive(Debug, Copy, Clone, Default)]
#[non_exhaustive]
pub struct open_how {
    /// An [`OFlags`] value represented as a `u64`.
    pub flags: u64,

    /// A [`Mode`] value represented as a `u64`.
    pub mode: u64,

    pub resolve: ResolveFlags,
}

impl open_how {
    /// Create a zero-initialized `Self`.
    pub const fn zeroed() -> Self {
        Self {
            flags: 0,
            mode: 0,
            resolve: ResolveFlags::empty(),
        }
    }
}

#[allow(missing_docs)]
#[repr(C)]
#[derive(Debug, Copy, Clone, Default)]
#[non_exhaustive]
pub struct io_uring_buf_reg {
    pub ring_addr: io_uring_ptr,
    pub ring_entries: u32,
    pub bgid: u16,
    pub flags: u16,
    #[doc(hidden)]
    pub resv: [u64; 3_usize],
}

#[allow(missing_docs)]
#[repr(C)]
#[derive(Debug, Copy, Clone, Default)]
#[non_exhaustive]
pub struct io_uring_buf {
    pub addr: io_uring_ptr,
    pub len: u32,
    pub bid: u16,
    #[doc(hidden)]
    pub resv: u16,
}

#[allow(missing_docs)]
#[repr(C)]
#[derive(Debug, Copy, Clone, Default)]
#[non_exhaustive]
pub struct buf_ring_tail_struct {
    #[doc(hidden)]
    pub resv1: u64,
    #[doc(hidden)]
    pub resv2: u32,
    #[doc(hidden)]
    pub resv3: u16,
    pub tail: u16,
}

#[allow(missing_docs)]
#[repr(C)]
#[derive(Debug, Default)]
pub struct buf_ring_bufs_struct {
    pub bufs: IncompleteArrayField<io_uring_buf>,
}

#[allow(missing_docs)]
#[repr(C)]
#[derive(Debug, Default)]
pub struct tail_or_bufs_struct {
    pub tail: UnionField<buf_ring_tail_struct>,
    pub bufs: UnionField<buf_ring_bufs_struct>,
    pub union_field: [u64; 2],
}

#[allow(missing_docs)]
#[repr(C)]
#[derive(Debug, Default)]
pub struct io_uring_buf_ring {
    pub tail_or_bufs: tail_or_bufs_struct,
}

#[allow(missing_docs)]
#[repr(C)]
#[derive(Debug, Default)]
#[non_exhaustive]
pub struct io_uring_napi {
    pub busy_poll_to: u32,
    pub prefer_busy_poll: u8,
    pub opcode: u8,
    #[doc(hidden)]
    pub pad: [u8; 2],
    pub op_param: u32,
    #[doc(hidden)]
    pub resv: u32,
}

#[allow(missing_docs)]
#[repr(C)]
#[derive(Debug, Default)]
#[non_exhaustive]
pub struct io_uring_clone_buffers {
    pub src_fd: u32,
    pub flags: u32,
    pub src_off: u32,
    pub dst_off: u32,
    pub nr: u32,
    #[doc(hidden)]
    pub pad: [u32; 3],
}

#[allow(missing_docs)]
#[repr(C)]
#[derive(Debug, Default)]
#[non_exhaustive]
pub struct io_uring_reg_wait {
    pub ts: Timespec,
    pub min_wait_usec: u32,
    pub flags: u32,
    pub sigmask: io_uring_ptr,
    pub sigmask_sz: u32,
    #[doc(hidden)]
    pub pad: [u32; 3],
    #[doc(hidden)]
    pub pad2: [u64; 2],
}

impl Default for ioprio_union {
    #[inline]
    fn default() -> Self {
        default_union!(ioprio_union, ioprio)
    }
}

impl Default for len_union {
    #[inline]
    fn default() -> Self {
        default_union!(len_union, len)
    }
}

impl Default for off_or_addr2_union {
    #[inline]
    fn default() -> Self {
        default_union!(off_or_addr2_union, off)
    }
}

impl Default for addr_or_splice_off_in_union {
    #[inline]
    fn default() -> Self {
        default_union!(addr_or_splice_off_in_union, splice_off_in)
    }
}

impl Default for addr3_or_cmd_union {
    #[inline]
    fn default() -> Self {
        default_union!(addr3_or_cmd_union, addr3)
    }
}

impl Default for op_flags_union {
    #[inline]
    fn default() -> Self {
        default_union!(op_flags_union, sync_range_flags)
    }
}

impl Default for buf_union {
    #[inline]
    fn default() -> Self {
        default_union!(buf_union, buf_index)
    }
}

impl Default for splice_fd_in_or_file_index_or_addr_len_union {
    #[inline]
    fn default() -> Self {
        default_union!(splice_fd_in_or_file_index_or_addr_len_union, splice_fd_in)
    }
}

impl Default for register_or_sqe_op_or_sqe_flags_union {
    #[inline]
    fn default() -> Self {
        default_union!(register_or_sqe_op_or_sqe_flags_union, sqe_flags)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::fd::AsRawFd as _;

    /// Check that our custom structs and unions have the same layout as the
    /// kernel's versions.
    #[test]
    fn io_uring_layouts() {
        use sys as c;

        // `io_uring_ptr` is a replacement for `u64`.
        assert_eq_size!(io_uring_ptr, u64);
        assert_eq_align!(io_uring_ptr, u64);

        // Test that pointers are stored in `io_uring_ptr` in the way that
        // io_uring stores them in a `u64`.
        unsafe {
            const MAGIC: u64 = !0x0123_4567_89ab_cdef;
            let ptr = io_uring_ptr::new(MAGIC as usize as *mut c_void);
            assert_eq!(ptr.ptr, MAGIC as usize as *mut c_void);
            #[cfg(target_pointer_width = "16")]
            assert_eq!(ptr.__pad16, 0);
            #[cfg(any(target_pointer_width = "16", target_pointer_width = "32"))]
            assert_eq!(ptr.__pad32, 0);
            let int = core::mem::transmute::<io_uring_ptr, u64>(ptr);
            assert_eq!(int, MAGIC as usize as u64);
        }

        // `io_uring_user_data` is a replacement for `u64`.
        assert_eq_size!(io_uring_user_data, u64);
        assert_eq_align!(io_uring_user_data, u64);

        // Test that `u64`s and pointers are properly stored in
        // `io_uring_user_data`.
        unsafe {
            const MAGIC: u64 = !0x0123_4567_89ab_cdef;
            let user_data = io_uring_user_data::from_u64(MAGIC);
            assert_eq!(user_data.u64_(), MAGIC);
            assert_eq!(
                core::mem::transmute::<io_uring_user_data, u64>(user_data),
                MAGIC
            );
            let user_data = io_uring_user_data::from_ptr(MAGIC as usize as *mut c_void);
            assert_eq!(user_data.ptr(), MAGIC as usize as *mut c_void);
            assert_eq!(
                core::mem::transmute::<io_uring_user_data, u64>(user_data),
                MAGIC as usize as u64
            );
        }

        check_renamed_type!(off_or_addr2_union, io_uring_sqe__bindgen_ty_1);
        check_renamed_type!(addr_or_splice_off_in_union, io_uring_sqe__bindgen_ty_2);
        check_renamed_type!(addr3_or_cmd_union, io_uring_sqe__bindgen_ty_6);
        check_renamed_type!(op_flags_union, io_uring_sqe__bindgen_ty_3);
        check_renamed_type!(buf_union, io_uring_sqe__bindgen_ty_4);
        check_renamed_type!(
            splice_fd_in_or_file_index_or_addr_len_union,
            io_uring_sqe__bindgen_ty_5
        );
        check_renamed_type!(addr_len_struct, io_uring_sqe__bindgen_ty_5__bindgen_ty_1);
        check_renamed_type!(
            register_or_sqe_op_or_sqe_flags_union,
            io_uring_restriction__bindgen_ty_1
        );

        check_renamed_type!(addr3_struct, io_uring_sqe__bindgen_ty_6__bindgen_ty_1);
        check_renamed_type!(cmd_op_struct, io_uring_sqe__bindgen_ty_1__bindgen_ty_1);

        check_type!(io_uring_sqe);
        check_struct_field!(io_uring_sqe, opcode);
        check_struct_field!(io_uring_sqe, flags);
        check_struct_field!(io_uring_sqe, ioprio);
        check_struct_field!(io_uring_sqe, fd);
        check_struct_renamed_field!(io_uring_sqe, off_or_addr2, __bindgen_anon_1);
        check_struct_renamed_field!(io_uring_sqe, addr_or_splice_off_in, __bindgen_anon_2);
        check_struct_field!(io_uring_sqe, len);
        check_struct_renamed_field!(io_uring_sqe, op_flags, __bindgen_anon_3);
        check_struct_field!(io_uring_sqe, user_data);
        check_struct_renamed_field!(io_uring_sqe, buf, __bindgen_anon_4);
        check_struct_field!(io_uring_sqe, personality);
        check_struct_renamed_field!(
            io_uring_sqe,
            splice_fd_in_or_file_index_or_addr_len,
            __bindgen_anon_5
        );
        check_struct_renamed_field!(io_uring_sqe, addr3_or_cmd, __bindgen_anon_6);

        check_type!(io_uring_restriction);
        check_struct_field!(io_uring_restriction, opcode);
        check_struct_renamed_field!(
            io_uring_restriction,
            register_or_sqe_op_or_sqe_flags,
            __bindgen_anon_1
        );
        check_struct_field!(io_uring_restriction, resv);
        check_struct_field!(io_uring_restriction, resv2);

        check_struct!(io_uring_cqe, user_data, res, flags, big_cqe);
        check_struct!(
            io_uring_params,
            sq_entries,
            cq_entries,
            flags,
            sq_thread_cpu,
            sq_thread_idle,
            features,
            wq_fd,
            resv,
            sq_off,
            cq_off
        );
        check_struct!(
            io_sqring_offsets,
            head,
            tail,
            ring_mask,
            ring_entries,
            flags,
            dropped,
            array,
            resv1,
            user_addr
        );
        check_struct!(
            io_cqring_offsets,
            head,
            tail,
            ring_mask,
            ring_entries,
            overflow,
            cqes,
            flags,
            resv1,
            user_addr
        );
        check_struct!(io_uring_recvmsg_out, namelen, controllen, payloadlen, flags);
        check_struct!(io_uring_probe, last_op, ops_len, resv, resv2, ops);
        check_struct!(io_uring_probe_op, op, resv, flags, resv2);
        check_struct!(io_uring_files_update, offset, resv, fds);
        check_struct!(io_uring_rsrc_register, nr, flags, resv2, data, tags);
        check_struct!(io_uring_rsrc_update, offset, resv, data);
        check_struct!(io_uring_rsrc_update2, offset, resv, data, tags, nr, resv2);
        check_struct!(
            io_uring_getevents_arg,
            sigmask,
            sigmask_sz,
            min_wait_usec,
            ts
        );
        check_struct!(iovec, iov_base, iov_len);
        check_struct!(open_how, flags, mode, resolve);
        check_struct!(io_uring_buf_reg, ring_addr, ring_entries, bgid, flags, resv);
        check_struct!(io_uring_buf, addr, len, bid, resv);
        check_struct!(
            io_uring_sync_cancel_reg,
            addr,
            fd,
            flags,
            timeout,
            opcode,
            pad,
            pad2
        );

        check_renamed_type!(tail_or_bufs_struct, io_uring_buf_ring__bindgen_ty_1);
        check_renamed_type!(
            buf_ring_tail_struct,
            io_uring_buf_ring__bindgen_ty_1__bindgen_ty_1
        );
        check_renamed_type!(
            buf_ring_bufs_struct,
            io_uring_buf_ring__bindgen_ty_1__bindgen_ty_2
        );
        check_struct_renamed_field!(io_uring_buf_ring, tail_or_bufs, __bindgen_anon_1);

        check_struct!(
            io_uring_napi,
            busy_poll_to,
            prefer_busy_poll,
            opcode,
            pad,
            op_param,
            resv
        );
        check_struct!(
            io_uring_clone_buffers,
            src_fd,
            flags,
            src_off,
            dst_off,
            nr,
            pad
        );
        check_struct!(
            io_uring_reg_wait,
            ts,
            min_wait_usec,
            flags,
            sigmask,
            sigmask_sz,
            pad,
            pad2
        );

        check_renamed_struct!(
            MsgHdr,
            msghdr,
            msg_name,
            msg_namelen,
            msg_iov,
            msg_iovlen,
            msg_control,
            msg_controllen,
            msg_flags
        );
    }

    #[test]
    fn test_io_uring_register_files_skip() {
        use crate::backend::c;
        assert!(IORING_REGISTER_FILES_SKIP.as_raw_fd() != -1);
        assert!(IORING_REGISTER_FILES_SKIP.as_raw_fd() != c::STDIN_FILENO);
        assert!(IORING_REGISTER_FILES_SKIP.as_raw_fd() != c::STDOUT_FILENO);
        assert!(IORING_REGISTER_FILES_SKIP.as_raw_fd() != c::STDERR_FILENO);
    }
}