Skip to content

Commit 50234dc

Browse files
committed
IoUring: implement outstanding flags and enumerations
Add SYNC_FILE_RANGE_* flags add XATTR_* flags Add Madvise for MADV_* constants Add Fadvise enum type for POSIX_FADV_* constants Signed-off-by: Bernard Assan <mega.alpha100@gmail.com>
1 parent e71a82d commit 50234dc

File tree

2 files changed

+175
-65
lines changed

2 files changed

+175
-65
lines changed

lib/std/os/linux.zig

Lines changed: 162 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,7 @@ pub const O = switch (native_arch) {
501501
pub const Pipe2 = switch (native_arch) {
502502
.x86_64, .x86, .riscv32, .riscv64, .loongarch64, .hexagon, .or1k, .s390x => packed struct(u32) {
503503
_: u7 = 0,
504+
/// Parameter to `pipe2` selecting notification pipe
504505
notification_pipe: bool = false,
505506
_9: u3 = 0,
506507
nonblock: bool = false,
@@ -512,6 +513,7 @@ pub const Pipe2 = switch (native_arch) {
512513
},
513514
.aarch64, .aarch64_be, .arm, .armeb, .thumb, .thumbeb, .m68k => packed struct(u32) {
514515
_: u7 = 0,
516+
/// Parameter to `pipe2` selecting notification pipe
515517
notification_pipe: bool = false,
516518
_9: u3 = 0,
517519
nonblock: bool = false,
@@ -523,6 +525,7 @@ pub const Pipe2 = switch (native_arch) {
523525
},
524526
.sparc64 => packed struct(u32) {
525527
_: u11 = 0,
528+
/// Parameter to `pipe2` selecting notification pipe
526529
notification_pipe: bool = false,
527530
_13: u2 = 0,
528531
nonblock: bool = false,
@@ -536,6 +539,7 @@ pub const Pipe2 = switch (native_arch) {
536539
_: u7 = 0,
537540
nonblock: bool = false,
538541
_9: u2 = 0,
542+
/// Parameter to `pipe2` selecting notification pipe
539543
notification_pipe: bool = false,
540544
_12: u4 = 0,
541545
direct: bool = false,
@@ -545,6 +549,7 @@ pub const Pipe2 = switch (native_arch) {
545549
},
546550
.powerpc, .powerpcle, .powerpc64, .powerpc64le => packed struct(u32) {
547551
_: u7 = 0,
552+
/// Parameter to `pipe2` selecting notification pipe
548553
notification_pipe: bool = false,
549554
_9: u3 = 0,
550555
nonblock: bool = false,
@@ -885,7 +890,6 @@ pub fn futex2_wake(
885890
/// Identical to `FUTEX.CMP_REQUEUE`, except it is part of the futex2 family of calls.
886891
///
887892
/// Requires at least kernel v6.7.
888-
// TODO: test to ensure I didn't break it
889893
pub fn futex2_requeue(
890894
/// The source and destination futexes. Must be a 2-element array.
891895
waiters: *const [2]Futex2.WaitOne,
@@ -4226,9 +4230,19 @@ pub const Shut = enum(u32) {
42264230
pub const RDWR: u32 = @intFromEnum(Shut.rdwr);
42274231
};
42284232

4229-
/// SYNC_FILE_RANGE_* flags
4233+
/// flags for `sync_file_range(2)` syscall
4234+
/// matches SYNC_FILE_RANGE_* in kernel
42304235
pub const SyncFileRange = packed struct(u32) {
4231-
_: u32 = 0, // TODO: fill out
4236+
wait_before: bool = false,
4237+
write: bool = false,
4238+
wait_after: bool = false,
4239+
_: u29 = 0,
4240+
4241+
pub const write_and_wait: SyncFileRange = .{
4242+
.wait_before = true,
4243+
.write = true,
4244+
.wait_after = true,
4245+
};
42324246
};
42334247

42344248
/// Deprecated alias to Sock
@@ -7050,9 +7064,18 @@ pub const Rename = packed struct(u32) {
70507064
_: u29 = 0,
70517065
};
70527066

7067+
/// By default (i.e, flags is .{}), the extended attribute will be created
7068+
/// if it does not exist, or the value will be replaced if the attribute
7069+
/// already exists. To modify this semantics, one of the fields in `SetXattr`
7070+
/// can be specified in flags. Matches XATTR_* in kernel
70537071
pub const SetXattr = packed struct(u32) {
7054-
_: u32 = 0, // TODO: add flags
7072+
/// set value, fail if attr already exists
7073+
create: bool = false,
7074+
/// set value, fail if attr does not exist
7075+
replace: bool = false,
7076+
_: u30 = 0,
70557077
};
7078+
70567079
pub const statx_timestamp = extern struct {
70577080
sec: i64,
70587081
nsec: u32,
@@ -8600,60 +8623,142 @@ pub const rlimit = extern struct {
86008623
max: rlim_t,
86018624
};
86028625

8603-
pub const MADV = struct {
8604-
pub const NORMAL = 0;
8605-
pub const RANDOM = 1;
8606-
pub const SEQUENTIAL = 2;
8607-
pub const WILLNEED = 3;
8608-
pub const DONTNEED = 4;
8609-
pub const FREE = 8;
8610-
pub const REMOVE = 9;
8611-
pub const DONTFORK = 10;
8612-
pub const DOFORK = 11;
8613-
pub const MERGEABLE = 12;
8614-
pub const UNMERGEABLE = 13;
8615-
pub const HUGEPAGE = 14;
8616-
pub const NOHUGEPAGE = 15;
8617-
pub const DONTDUMP = 16;
8618-
pub const DODUMP = 17;
8619-
pub const WIPEONFORK = 18;
8620-
pub const KEEPONFORK = 19;
8621-
pub const COLD = 20;
8622-
pub const PAGEOUT = 21;
8623-
pub const HWPOISON = 100;
8624-
pub const SOFT_OFFLINE = 101;
8625-
};
8626-
8627-
pub const Madvice = enum(u32) {
8628-
_, // TODO: add options
8629-
};
8630-
pub const Fadvice = enum(u32) {
8631-
_, // TODO: add options
8632-
};
8633-
8634-
pub const POSIX_FADV = switch (native_arch) {
8635-
.s390x => if (@typeInfo(usize).int.bits == 64) struct {
8636-
pub const NORMAL = 0;
8637-
pub const RANDOM = 1;
8638-
pub const SEQUENTIAL = 2;
8639-
pub const WILLNEED = 3;
8640-
pub const DONTNEED = 6;
8641-
pub const NOREUSE = 7;
8642-
} else struct {
8643-
pub const NORMAL = 0;
8644-
pub const RANDOM = 1;
8645-
pub const SEQUENTIAL = 2;
8646-
pub const WILLNEED = 3;
8647-
pub const DONTNEED = 4;
8648-
pub const NOREUSE = 5;
8626+
/// DEPRECATED alias for Madvise
8627+
pub const MADV = Madvise;
8628+
8629+
// COMMIT: update MADV_* flags and type as enum
8630+
/// advice flags for `madvise`
8631+
/// matches MADV_* in kernel
8632+
pub const Madvise = enum(u32) {
8633+
/// no further special treatment
8634+
normal = 0,
8635+
/// expect random page references
8636+
random = 1,
8637+
/// expect sequential page references
8638+
sequential = 2,
8639+
/// will need these pages
8640+
willneed = 3,
8641+
/// don't need these pages
8642+
dontneed = 4,
8643+
/// free pages only if memory pressure
8644+
free = 8,
8645+
/// remove these pages & resources
8646+
remove = 9,
8647+
/// don't inherit across fork
8648+
dontfork = 10,
8649+
/// do inherit across fork
8650+
dofork = 11,
8651+
/// KSM may merge identical pages
8652+
mergeable = 12,
8653+
/// KSM may not merge identical pages
8654+
unmergeable = 13,
8655+
/// Worth backing with hugepages
8656+
hugepage = 14,
8657+
/// Not worth backing with hugepages
8658+
nohugepage = 15,
8659+
/// Explicity exclude from the core dump, overrides the coredump filter bits
8660+
dontdump = 16,
8661+
/// Clear the MADV_DONTDUMP flag
8662+
dodump = 17,
8663+
/// Zero memory on fork, child only
8664+
wipeonfork = 18,
8665+
/// Undo MADV_WIPEONFORK
8666+
keeponfork = 19,
8667+
/// deactivate these pages
8668+
cold = 20,
8669+
/// reclaim these pages
8670+
pageout = 21,
8671+
/// populate (prefault) page tables readable
8672+
populate_read = 22,
8673+
/// populate (prefault) page tables writable
8674+
populate_write = 23,
8675+
/// like DONTNEED, but drop locked pages too
8676+
dontneed_locked = 24,
8677+
/// Synchronous hugepage collapse
8678+
collapse = 25,
8679+
/// poison a page for testing
8680+
hwpoison = 100,
8681+
/// soft offline page for testing
8682+
soft_offline = 101,
8683+
/// fatal signal on access to range
8684+
guard_install = 102,
8685+
/// unguard range
8686+
guard_remove = 103,
8687+
_,
8688+
8689+
// DEPRECATED aliases for `Madvise`
8690+
pub const NORMAL: u32 = @intFromEnum(Madvise.normal);
8691+
pub const RANDOM: u32 = @intFromEnum(Madvise.random);
8692+
pub const SEQUENTIAL: u32 = @intFromEnum(Madvise.sequential);
8693+
pub const WILLNEED: u32 = @intFromEnum(Madvise.willneed);
8694+
pub const DONTNEED: u32 = @intFromEnum(Madvise.dontneed);
8695+
pub const FREE: u32 = @intFromEnum(Madvise.free);
8696+
pub const REMOVE: u32 = @intFromEnum(Madvise.remove);
8697+
pub const DONTFORK: u32 = @intFromEnum(Madvise.dontfork);
8698+
pub const DOFORK: u32 = @intFromEnum(Madvise.dofork);
8699+
pub const MERGEABLE: u32 = @intFromEnum(Madvise.mergeable);
8700+
pub const UNMERGEABLE: u32 = @intFromEnum(Madvise.unmergeable);
8701+
pub const HUGEPAGE: u32 = @intFromEnum(Madvise.hugepage);
8702+
pub const NOHUGEPAGE: u32 = @intFromEnum(Madvise.nohugepage);
8703+
pub const DONTDUMP: u32 = @intFromEnum(Madvise.dontdump);
8704+
pub const DODUMP: u32 = @intFromEnum(Madvise.dodump);
8705+
pub const WIPEONFORK: u32 = @intFromEnum(Madvise.wipeonfork);
8706+
pub const KEEPONFORK: u32 = @intFromEnum(Madvise.keeponfork);
8707+
pub const COLD: u32 = @intFromEnum(Madvise.cold);
8708+
pub const PAGEOUT: u32 = @intFromEnum(Madvise.pageout);
8709+
pub const HWPOISON: u32 = @intFromEnum(Madvise.hwpoison);
8710+
pub const SOFT_OFFLINE: u32 = @intFromEnum(Madvise.soft_offline);
8711+
};
8712+
8713+
/// DEPRECATED alias to Fadvice
8714+
pub const POSIX_FADV = Fadvise;
8715+
8716+
/// advice flags for `posix_fadvise`
8717+
/// matches POSIX_FADV_* in kernel
8718+
pub const Fadvise = switch (native_arch) {
8719+
.s390x => if (@typeInfo(usize).int.bits == 64) enum(u32) {
8720+
/// No further special treatment
8721+
normal = 0,
8722+
/// Expect random page references
8723+
random = 1,
8724+
/// Expect sequential page references
8725+
sequential = 2,
8726+
/// Will need these pages
8727+
willneed = 3,
8728+
/// Don't need these pages
8729+
dontneed = 6,
8730+
/// Data will be accessed once
8731+
noreuse = 7,
8732+
_,
8733+
8734+
pub const NORMAL: u32 = @intFromEnum(Fadvise.normal);
8735+
pub const RANDOM: u32 = @intFromEnum(Fadvise.random);
8736+
pub const SEQUENTIAL: u32 = @intFromEnum(Fadvise.sequential);
8737+
pub const WILLNEED: u32 = @intFromEnum(Fadvise.willneed);
8738+
pub const DONTNEED: u32 = @intFromEnum(Fadvise.dontneed);
8739+
pub const NOREUSE: u32 = @intFromEnum(Fadvise.noreuse);
86498740
},
8650-
else => struct {
8651-
pub const NORMAL = 0;
8652-
pub const RANDOM = 1;
8653-
pub const SEQUENTIAL = 2;
8654-
pub const WILLNEED = 3;
8655-
pub const DONTNEED = 4;
8656-
pub const NOREUSE = 5;
8741+
else => enum(u32) {
8742+
/// No further special treatment
8743+
normal = 0,
8744+
/// Expect random page references
8745+
random = 1,
8746+
/// Expect sequential page references
8747+
sequential = 2,
8748+
/// Will need these pages
8749+
willneed = 3,
8750+
/// Don't need these pages
8751+
dontneed = 4,
8752+
/// Data will be accessed once
8753+
noreuse = 5,
8754+
_,
8755+
8756+
pub const NORMAL: u32 = @intFromEnum(Fadvise.normal);
8757+
pub const RANDOM: u32 = @intFromEnum(Fadvise.random);
8758+
pub const SEQUENTIAL: u32 = @intFromEnum(Fadvise.sequential);
8759+
pub const WILLNEED: u32 = @intFromEnum(Fadvise.willneed);
8760+
pub const DONTNEED: u32 = @intFromEnum(Fadvise.dontneed);
8761+
pub const NOREUSE: u32 = @intFromEnum(Fadvise.noreuse);
86578762
},
86588763
};
86598764

lib/std/os/linux/IoUring.zig

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1137,7 +1137,7 @@ pub fn fadvice(
11371137
fd: linux.fd_t,
11381138
offset: u64,
11391139
len: u32,
1140-
advice: linux.Fadvice,
1140+
advice: linux.Fadvise,
11411141
) !*Sqe {
11421142
const sqe = try self.get_sqe();
11431143
sqe.prep_fadvice(fd, offset, len, advice);
@@ -1151,7 +1151,7 @@ pub fn madvice(
11511151
self: *IoUring,
11521152
user_data: u64,
11531153
memory: []u8,
1154-
advice: linux.Fadvice,
1154+
advice: linux.Madvise,
11551155
) !*Sqe {
11561156
const sqe = try self.get_sqe();
11571157
sqe.prep_madvice(memory, advice);
@@ -1351,7 +1351,13 @@ pub fn provide_buffers(
13511351
buffer_id: usize,
13521352
) !*Sqe {
13531353
const sqe = try self.get_sqe();
1354-
sqe.prep_provide_buffers(buffers, buffer_size, buffers_count, group_id, buffer_id);
1354+
sqe.prep_provide_buffers(
1355+
buffers,
1356+
buffer_size,
1357+
buffers_count,
1358+
group_id,
1359+
buffer_id,
1360+
);
13551361
sqe.user_data = user_data;
13561362
return sqe;
13571363
}
@@ -1449,7 +1455,7 @@ pub fn sync_file_range(
14491455
fd: linux.fd_t,
14501456
len: u32,
14511457
offset: u64,
1452-
flags: linux.SyncFileRange, // TODO: add flags
1458+
flags: linux.SyncFileRange,
14531459
) !*Sqe {
14541460
const sqe = try self.get_sqe();
14551461
sqe.prep_sync_file_range(fd, len, offset, flags);
@@ -2220,7 +2226,6 @@ pub fn getsockopt(
22202226
);
22212227
}
22222228

2223-
// TODO: move buf_ring fns into BufferRing type
22242229
/// Registers a shared buffer ring to be used with provided buffers. `entries`
22252230
/// number of `io_uring_buf` structures is mem mapped and shared by kernel.
22262231
///
@@ -2967,7 +2972,7 @@ pub const Sqe = extern struct {
29672972
fd: linux.fd_t,
29682973
offset: u64,
29692974
len: u32,
2970-
advice: linux.Fadvice,
2975+
advice: linux.Fadvise,
29712976
) void {
29722977
sqe.prep_rw(.fadvise, fd, undefined, len, offset);
29732978
sqe.rw_flags = @intFromEnum(advice);
@@ -2976,7 +2981,7 @@ pub const Sqe = extern struct {
29762981
pub fn prep_madvice(
29772982
sqe: *Sqe,
29782983
memory: []u8,
2979-
advice: linux.Madvice,
2984+
advice: linux.Madvise,
29802985
) void {
29812986
sqe.prep_rw(.madvise, -1, @intFromPtr(memory.ptr), memory.len, 0);
29822987
sqe.rw_flags = @intFromEnum(advice);
@@ -3047,7 +3052,7 @@ pub const Sqe = extern struct {
30473052
fd: linux.fd_t,
30483053
len: u32,
30493054
offset: u64,
3050-
flags: linux.SyncFileRange, // TODO: add flags
3055+
flags: linux.SyncFileRange,
30513056
) void {
30523057
sqe.prep_rw(.sync_file_range, fd, undefined, len, offset);
30533058
sqe.rw_flags = @bitCast(flags);

0 commit comments

Comments
 (0)