authorgravatar for vincent@rischmann.frVincent Rischmann <vincent@rischmann.fr> 2021-11-08 23:59:03+01:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-11-09 13:35:17-05:00
log082072bd4f4613e798068370681c269860785cbf
tree7eb4c1d793720f40e98fc1bb42ccf5fe9e95df7e
parent4d75382452abd5e50e6f8ca9c82b67fbe0983456

os/linux/io_uring: implement statx


1 files changed, 83 insertions(+), 0 deletions(-)

lib/std/os/linux/io_uring.zig+83
......@@ -650,6 +650,23 @@ pub const IO_Uring = struct {
650650 return sqe;
651651 }
652652
653 /// Queues (but does not submit) an SQE to perform an `statx(2)`.
654 /// Returns a pointer to the SQE.
655 pub fn statx(
656 self: *IO_Uring,
657 user_data: u64,
658 fd: os.fd_t,
659 path: [:0]const u8,
660 flags: u32,
661 mask: u32,
662 buf: *linux.Statx,
663 ) !*io_uring_sqe {
664 const sqe = try self.get_sqe();
665 io_uring_prep_statx(sqe, fd, path, flags, mask, buf);
666 sqe.user_data = user_data;
667 return sqe;
668 }
669
653670 /// Registers an array of file descriptors.
654671 /// Every time a file descriptor is put in an SQE and submitted to the kernel, the kernel must
655672 /// retrieve a reference to the file, and once I/O has completed the file reference must be
......@@ -1116,6 +1133,18 @@ pub fn io_uring_prep_fallocate(
11161133 };
11171134}
11181135
1136pub fn io_uring_prep_statx(
1137 sqe: *io_uring_sqe,
1138 fd: os.fd_t,
1139 path: [*:0]const u8,
1140 flags: u32,
1141 mask: u32,
1142 buf: *linux.Statx,
1143) void {
1144 io_uring_prep_rw(.STATX, sqe, fd, @ptrToInt(path), mask, @ptrToInt(buf));
1145 sqe.rw_flags = flags;
1146}
1147
11191148test "structs/offsets/entries" {
11201149 if (builtin.os.tag != .linux) return error.SkipZigTest;
11211150
......@@ -1720,3 +1749,57 @@ test "fallocate" {
17201749
17211750 try testing.expectEqual(len, (try file.stat()).size);
17221751}
1752
1753test "statx" {
1754 if (builtin.os.tag != .linux) return error.SkipZigTest;
1755
1756 var ring = IO_Uring.init(1, 0) catch |err| switch (err) {
1757 error.SystemOutdated => return error.SkipZigTest,
1758 error.PermissionDenied => return error.SkipZigTest,
1759 else => return err,
1760 };
1761 defer ring.deinit();
1762
1763 const path = "test_io_uring_statx";
1764 const file = try std.fs.cwd().createFile(path, .{ .truncate = true, .mode = 0o666 });
1765 defer file.close();
1766 defer std.fs.cwd().deleteFile(path) catch {};
1767
1768 try testing.expectEqual(@as(u64, 0), (try file.stat()).size);
1769
1770 try file.writeAll("foobar");
1771
1772 var buf: linux.Statx = undefined;
1773 const sqe = try ring.statx(
1774 0xaaaaaaaa,
1775 linux.AT.FDCWD,
1776 path,
1777 0,
1778 linux.STATX_SIZE,
1779 &buf,
1780 );
1781 try testing.expectEqual(linux.IORING_OP.STATX, sqe.opcode);
1782 try testing.expectEqual(@as(i32, linux.AT.FDCWD), sqe.fd);
1783 try testing.expectEqual(@as(u32, 1), try ring.submit());
1784
1785 const cqe = try ring.copy_cqe();
1786 switch (cqe.err()) {
1787 .SUCCESS => {},
1788 // This kernel's io_uring does not yet implement statx():
1789 .INVAL => return error.SkipZigTest,
1790 // This kernel does not implement statx():
1791 .NOSYS => return error.SkipZigTest,
1792 // The filesystem containing the file referred to by fd does not support this operation;
1793 // or the mode is not supported by the filesystem containing the file referred to by fd:
1794 .OPNOTSUPP => return error.SkipZigTest,
1795 else => |errno| std.debug.panic("unhandled errno: {}", .{errno}),
1796 }
1797 try testing.expectEqual(linux.io_uring_cqe{
1798 .user_data = 0xaaaaaaaa,
1799 .res = 0,
1800 .flags = 0,
1801 }, cqe);
1802
1803 try testing.expect(buf.mask & os.linux.STATX_SIZE == os.linux.STATX_SIZE);
1804 try testing.expectEqual(@as(u64, 6), buf.size);
1805}