Issue a failing Identify into a buffer filled with a sentinel and verify that the target transfers no data, with a successful Identify and a namespace read as controls. The test runs on every configured transport; only nvmet-fc failed it before the fix. Add a small helper because nvme-cli cannot prefill the data buffer. The test fails over fc on the unpatched kernel and passes with the fix applied. Assisted-by: LLM Signed-off-by: Andrea Parri --- src/.gitignore | 1 + src/Makefile | 1 + src/nvme-sentinel-read.c | 89 ++++++++++++++++++++++++++++++++++++++++ tests/nvme/071 | 80 ++++++++++++++++++++++++++++++++++++ tests/nvme/071.out | 2 + 5 files changed, 173 insertions(+) create mode 100644 src/nvme-sentinel-read.c create mode 100755 tests/nvme/071 create mode 100644 tests/nvme/071.out diff --git a/src/.gitignore b/src/.gitignore index e9869e172b20f..2429da9d9cf44 100644 --- a/src/.gitignore +++ b/src/.gitignore @@ -18,3 +18,4 @@ /ioctl-lbmd-query /nvme-passthru-admin-uring /nvme-delay-ioctl +/nvme-sentinel-read diff --git a/src/Makefile b/src/Makefile index dd646940fa25e..78433f0762b4a 100644 --- a/src/Makefile +++ b/src/Makefile @@ -24,6 +24,7 @@ C_TARGETS := \ mount_clear_sock \ nvme-delay-ioctl \ nvme-passthrough-meta \ + nvme-sentinel-read \ ioctl-lbmd-query \ nbdsetsize \ openclose \ diff --git a/src/nvme-sentinel-read.c b/src/nvme-sentinel-read.c new file mode 100644 index 0000000000000..dd36f283f46e2 --- /dev/null +++ b/src/nvme-sentinel-read.c @@ -0,0 +1,89 @@ +// SPDX-License-Identifier: GPL-3.0+ +// Copyright (C) 2026 Andrea Parri + +/* + * Issue an NVMe admin read command into a buffer filled with a sentinel and + * print the NVMe status and the number of bytes the command overwrote. + */ +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define SENTINEL 0xa5 + +static int parse_u32(const char *s, uint32_t *val) +{ + unsigned long v; + char *end; + + errno = 0; + v = strtoul(s, &end, 0); + if (errno || end == s || *end || v > UINT32_MAX) + return -1; + *val = v; + return 0; +} + +int main(int argc, char **argv) +{ + struct nvme_passthru_cmd cmd = { }; + uint32_t opcode, cdw10, len, i, changed = 0; + unsigned char *buf; + void *p; + int fd, ret; + + if (argc != 5 || parse_u32(argv[2], &opcode) || opcode > 0xff || + parse_u32(argv[3], &cdw10) || parse_u32(argv[4], &len) || !len) { + fprintf(stderr, "usage: %s /dev/nvmeX opcode cdw10 data-len\n", + argv[0]); + return 2; + } + + /* + * Page-align the buffer so that the kernel maps it for the transfer + * instead of bouncing it: a bounce buffer is zero-filled and copied + * back even when the command fails, which would hide the sentinel. + */ + ret = posix_memalign(&p, 4096, len); + if (ret) { + fprintf(stderr, "posix_memalign: %s\n", strerror(ret)); + return 2; + } + buf = p; + memset(buf, SENTINEL, len); + + fd = open(argv[1], O_RDONLY); + if (fd < 0) { + perror(argv[1]); + free(buf); + return 2; + } + + cmd.opcode = opcode; + cmd.addr = (uintptr_t)buf; + cmd.data_len = len; + cmd.cdw10 = cdw10; + + ret = ioctl(fd, NVME_IOCTL_ADMIN_CMD, &cmd); + close(fd); + if (ret < 0) { + perror("NVME_IOCTL_ADMIN_CMD"); + free(buf); + return 2; + } + + for (i = 0; i < len; i++) + if (buf[i] != SENTINEL) + changed++; + + printf("%#x %u\n", ret, changed); + free(buf); + return 0; +} diff --git a/tests/nvme/071 b/tests/nvme/071 new file mode 100755 index 0000000000000..5333bfac8db53 --- /dev/null +++ b/tests/nvme/071 @@ -0,0 +1,80 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-3.0+ +# Copyright (C) 2026 Andrea Parri +# +# Check that the NVMe target transfers no read data for a failed command. +# The host buffer is filled with a sentinel, so any data the target sends +# shows up as changed bytes. +# +# Regression test for patch "nvmet-fc: only send read data for successful +# commands". + +. tests/nvme/rc + +DESCRIPTION="verify nvmet does not transfer data for failed reads" +QUICK=1 + +requires() { + _nvme_requires + _have_loop + _require_kernel_nvme_target + _have_src_program nvme-sentinel-read +} + +set_conditions() { + _set_nvme_trtype "$@" +} + +# Issue an Identify with CNS $2 to controller $1 and print the NVMe status +# and the number of changed bytes in the 4096-byte data buffer. +_identify() { + src/nvme-sentinel-read "$1" 0x06 "$2" 4096 2>>"$FULL" +} + +test() { + echo "Running ${TEST_NAME}" + + local changed ctrl ns status + + _setup_nvmet + # File backing: the namespace read below fails over loop with the + # default loop device backing, for reasons unrelated to this test. + _nvmet_target_setup --blkdev file + _nvme_connect_subsys + + ctrl=$(_find_nvme_dev "$def_subsysnqn") + ns="/dev/${ctrl}n1" + + # Identify Controller succeeds and must overwrite the sentinel. + if ! read -r status changed < <(_identify "/dev/$ctrl" 0x01); then + echo "successful Identify could not be issued" + else + echo "cns=0x01 status=$status changed=$changed" >>"$FULL" + if ((status != 0 || changed == 0)); then + echo "successful Identify returned status $status with $changed bytes" + fi + fi + + # An unsupported CNS fails and must leave the sentinel intact. + if ! read -r status changed < <(_identify "/dev/$ctrl" 0xff); then + echo "failed Identify could not be issued" + else + echo "cns=0xff status=$status changed=$changed" >>"$FULL" + if (((status & 0x7ff) != 2)); then + echo "failed Identify returned status $status, expected 0x2" + fi + if ((changed != 0)); then + echo "failed Identify transferred data: $changed bytes changed" + fi + fi + + if ! dd if="$ns" of=/dev/null bs=4096 count=256 iflag=direct \ + status=none >>"$FULL" 2>&1; then + echo "namespace read failed" + fi + + _nvme_disconnect_subsys + _nvmet_target_cleanup + + echo "Test complete" +} diff --git a/tests/nvme/071.out b/tests/nvme/071.out new file mode 100644 index 0000000000000..146809b91e0a3 --- /dev/null +++ b/tests/nvme/071.out @@ -0,0 +1,2 @@ +Running nvme/071 +Test complete -- 2.53.0