Skip to contentSkip to conversation

liburing: add test for metadata

PATCH In review Keith Busch 2 participants 2 messages Nov 7, 2025 – Nov 8, 2025 io_uring

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 liburing: add test for metadata

From: Keith Busch <[email protected]>

If the test device supports metadata, try attaching a pi buffer with various page offsets, and seed offsets. If the metadata contains opaque data preceding or following the data integrity field, fill it with a pattern of data, and verify the expected data and metadata matches on the other side.

The sizes and offsets send should guarantee kernel splits and bounce buffers will get used.

At the end of the test, the written blocks are overwritten without providing metadata. This test doesn't calculate the guard tags, so writing without metadata lets the kernel generate the expected guard and ref tags so that buffered IO won't get seemingly unexpected failures.

Tested on qemu nvme with 512b and 4k logical block size with 8, 16, and 64 metadata, both with and without pi offsets for the larger formats.

---
 src/include/liburing/io_uring.h |  16 ++
 test/Makefile                   |   1 +
 test/metadata.c                 | 404 ++++++++++++++++++++++++++++++++
 3 files changed, 421 insertions(+)
 create mode 100644 test/metadata.c
src/include/liburing/io_uring.h +16 -0
index 44ce8229..a54e5b42 100644
@@ -100,6 +100,10 @@ struct io_uring_sqe {
 			__u64	addr3;
 			__u64	__pad2[1];
 		};
+		struct {
+			__u64   attr_ptr; /* pointer to attribute information */
+			__u64   attr_type_mask; /* bit mask of attributes */
+                };
 		__u64	optval;
 		/*
 		 * If the ring is initialized with IORING_SETUP_SQE128, then
@@ -109,6 +113,18 @@ struct io_uring_sqe {
 	};
 };
 
+/* sqe->attr_type_mask flags */
+#define IORING_RW_ATTR_FLAG_PI  (1U << 0)
+/* PI attribute information */
+struct io_uring_attr_pi {
+		__u16	flags;
+		__u16	app_tag;
+		__u32	len;
+		__u64	addr;
+		__u64	seed;
+		__u64	rsvd;
+};
+
 /*
  * If sqe->file_index is set to this for opcodes that instantiate a new
  * direct descriptor (like openat/openat2/accept), then io_uring will allocate
test/Makefile +1 -0
index ee983680..8c4c6db5 100644
@@ -141,6 +141,7 @@ test_srcs := \
 	link-timeout.c \
 	linked-defer-close.c \
 	madvise.c \
+	metadata.c \
 	min-timeout.c \
 	min-timeout-wait.c \
 	mkdir.c \
test/metadata.c new file +404 -0

404 lines not on this page · raw message

Signature · 1 line
2.47.3
maintainer
#1 lore raw

test/metadata.c line 236

Quoted 12 lines · Keith Busch
test/metadata.c new file +12 -0
+int main(int argc, char *argv[])
+{
+	int fd, ret, offset, intervals, metabuffer_size, metabuffer_tx_size;
+	void *orig_data_buf, *orig_pi_buf, *data_buf;
+	struct io_uring_sqe *sqe;
+	struct io_uring_cqe *cqe;
+	struct io_uring ring;
+
+	if (argc < 2) {
+		fprintf(stderr, "Usage: %s <dev>\n", argv[0]);
+		return T_EXIT_FAIL;
+	}

This should be a T_EXIT_SKIP.

Quoted 147 lines
test/metadata.c new file +147 -0
+	fd = open(argv[1], O_RDWR | O_DIRECT);
+	if (fd < 0) {
+		perror("Failed to open device with O_DIRECT");
+		return T_EXIT_FAIL;
+	}
+
+	ret = init_capabilities(fd);
+	if (ret < 0)
+		return T_EXIT_FAIL;
+	if (lba_size == 0 || metadata_size == 0)
+		return T_EXIT_SKIP;
+
+	intervals = DATA_SIZE / lba_size;
+	metabuffer_tx_size = intervals * metadata_size;
+	metabuffer_size = metabuffer_tx_size * 2;
+
+	if (posix_memalign(&orig_data_buf, pagesize, DATA_SIZE)) {
+		perror("posix_memalign failed for data buffer");
+		ret = T_EXIT_FAIL;
+		goto close;
+	}
+
+	if (posix_memalign(&orig_pi_buf, pagesize, metabuffer_size)) {
+		perror("posix_memalign failed for metadata buffer");
+		ret = T_EXIT_FAIL;
+		goto free;
+	}
+
+	ret = io_uring_queue_init(8, &ring, 0);
+	if (ret < 0) {
+		perror("io_uring_queue_init failed");
+		goto cleanup;
+	}
+
+	data_buf = orig_data_buf;
+	for (offset = 0; offset < 512; offset++) {
+		void *pi_buf = (char *)orig_pi_buf + offset * 4;
+		struct io_uring_attr_pi pi_attr = {
+			.addr = (__u64)pi_buf,
+			.seed = offset,
+			.len = metabuffer_tx_size,
+		};
+
+		if (reftag_enabled)
+			pi_attr.flags = IO_INTEGRITY_CHK_REFTAG;
+
+		init_data(data_buf, offset);
+		init_metadata(pi_buf, intervals, offset);
+
+		sqe = io_uring_get_sqe(&ring);
+		if (!sqe) {
+			fprintf(stderr, "Failed to get SQE\n");
+			ret = T_EXIT_FAIL;
+			goto ring_exit;
+		}
+
+		io_uring_prep_write(sqe, fd, data_buf, DATA_SIZE, offset * lba_size * 8);
+		io_uring_sqe_set_data(sqe, (void *)1L);
+
+		sqe->attr_type_mask = IORING_RW_ATTR_FLAG_PI;
+		sqe->attr_ptr = (__u64)&pi_attr;
+
+		ret = io_uring_submit(&ring);
+		if (ret < 1) {
+			perror("io_uring_submit failed (WRITE)");
+			ret = T_EXIT_FAIL;
+			goto ring_exit;
+		}
+
+		ret = io_uring_wait_cqe(&ring, &cqe);
+		if (ret < 0) {
+			perror("io_uring_wait_cqe failed (WRITE)");
+			ret = T_EXIT_FAIL;
+			goto ring_exit;
+		}
+
+		if (cqe->res < 0) {
+			fprintf(stderr, "write failed at offset %d: %s\n",
+				offset, strerror(-cqe->res));
+			ret = T_EXIT_FAIL;
+			goto ring_exit;
+		}
+
+		io_uring_cqe_seen(&ring, cqe);
+
+		memset(data_buf, 0, DATA_SIZE);
+		memset(pi_buf, 0, metabuffer_tx_size);
+
+		sqe = io_uring_get_sqe(&ring);
+		if (!sqe) {
+			fprintf(stderr, "failed to get SQE\n");
+			ret = T_EXIT_FAIL;
+			goto ring_exit;
+		}
+
+		io_uring_prep_read(sqe, fd, data_buf, DATA_SIZE, offset * lba_size * 8);
+		io_uring_sqe_set_data(sqe, (void *)2L);
+
+		sqe->attr_type_mask = IORING_RW_ATTR_FLAG_PI;
+		sqe->attr_ptr = (__u64)&pi_attr;
+
+		ret = io_uring_submit(&ring);
+		if (ret < 1) {
+			perror("io_uring_submit failed (read)");
+			ret = T_EXIT_FAIL;
+			goto ring_exit;
+		}
+
+		ret = io_uring_wait_cqe(&ring, &cqe);
+		if (ret < 0) {
+			fprintf(stderr, "io_uring_wait_cqe failed (read): %s\n", strerror(-ret));
+			ret = T_EXIT_FAIL;
+			goto ring_exit;
+		}
+
+		if (cqe->res < 0) {
+			fprintf(stderr, "read failed at offset %d: %s\n",
+				offset, strerror(-cqe->res));
+			ret = T_EXIT_FAIL;
+			goto ring_exit;
+		}
+
+		ret = check_data(data_buf, offset);
+		if (ret) {
+			fprintf(stderr, "data corruption at offset %d\n",
+				offset);
+			ret = T_EXIT_FAIL;
+			goto ring_exit;
+		}
+
+		ret = check_metadata(pi_buf, intervals, offset);
+		if (ret) {
+			fprintf(stderr, "metadata corruption at offset %d\n",
+				offset);
+			ret = T_EXIT_FAIL;
+			goto ring_exit;
+		}
+
+		io_uring_cqe_seen(&ring, cqe);
+	}
+
+	memset(data_buf, 0, DATA_SIZE);
+
+	sqe = io_uring_get_sqe(&ring);
+	io_uring_prep_write(sqe, fd, data_buf, DATA_SIZE, 0);
+	io_uring_sqe_set_data(sqe, (void *)1L);
+

test/metadata.c line 394

Quoted 10 lines
test/metadata.c new file +10 -0
+	sqe = io_uring_get_sqe(&ring);
+	io_uring_prep_write(sqe, fd, data_buf, DATA_SIZE, DATA_SIZE);
+	io_uring_sqe_set_data(sqe, (void *)2L);
+
+	io_uring_submit(&ring);
+
+	io_uring_wait_cqe(&ring, &cqe);
+	io_uring_cqe_seen(&ring, cqe);
+	io_uring_wait_cqe(&ring, &cqe);
+	io_uring_cqe_seen(&ring, cqe);

This looks a bit odd - no error checking?

Signature · 1 line
Jens Axboe