From fb49190ac456021fc5a8956cd7940dbc349b6be8 Mon Sep 17 00:00:00 2001 From: Martin Belanger Date: Mon, 30 Nov 2026 07:00:38 -0500 Subject: [PATCH 1/3] libnvme: derive the hostid when emitting a hostnqn without one The config reader rejects a [Host] section with a hostnqn and no hostid. "nvme config create -q " without "-I" wrote such a section, and every later read of the configuration failed. Take the hostid from a UUID-based hostnqn, as nvme connect does. Reject any other hostnqn without a hostid. Signed-off-by: Martin Belanger --- libnvme/src/nvme/config-emit.c | 14 +++++++ libnvme/src/nvme/config.h | 8 ++-- libnvme/tests/config-emit.c | 60 +++++++++++++++++++++++++++ tests/cli/nvme_config_convert_test.py | 20 +++++++++ tests/cli/nvme_config_create_test.py | 20 +++++++++ 5 files changed, 119 insertions(+), 3 deletions(-) diff --git a/libnvme/src/nvme/config-emit.c b/libnvme/src/nvme/config-emit.c index 48cbb7ddbe..c219b239e2 100644 --- a/libnvme/src/nvme/config-emit.c +++ b/libnvme/src/nvme/config-emit.c @@ -29,6 +29,7 @@ #include #include +#include #include #include @@ -187,6 +188,7 @@ __shr_public int libnvmf_config_emit_add( const char *hostnqn, const char *hostid, const struct libnvmf_params *params, const char *hostsymname) { + __cleanup_free char *derived = NULL; struct emit_persona *persona; struct emit_conn *conn; int ret; @@ -205,6 +207,18 @@ __shr_public int libnvmf_config_emit_add( return -EINVAL; } + /* The reader rejects a hostnqn without a hostid */ + if (hostnqn && !hostid) { + derived = libnvme_hostid_from_hostnqn(hostnqn); + if (!derived || !shr_hostid_valid(derived)) { + emit_err(emitter->ctx, + "config emit: hostnqn %s requires a hostid", + hostnqn); + return -EINVAL; + } + hostid = derived; + } + ret = check_persona_identity(emitter, hostnqn, hostid); if (ret) return ret; diff --git a/libnvme/src/nvme/config.h b/libnvme/src/nvme/config.h index cc0177f8f3..4c76b4c88a 100644 --- a/libnvme/src/nvme/config.h +++ b/libnvme/src/nvme/config.h @@ -451,7 +451,8 @@ void libnvmf_config_emit_free(struct libnvmf_config_emitter *emitter); * @host_traddr: Host transport address, or NULL. * @host_iface: Host interface, or NULL. * @hostnqn: Host NQN, or NULL. - * @hostid: Host identifier, or NULL. + * @hostid: Host identifier, or NULL. If NULL and @hostnqn is set, + * the UUID of a UUID-based @hostnqn is used. * @params: Connection parameters, or NULL. * @hostsymname: Host symbolic name, or NULL. * @@ -459,8 +460,9 @@ void libnvmf_config_emit_free(struct libnvmf_config_emitter *emitter); * * Return: * * 0 on success. - * * -EINVAL if a required parameter is missing or if @hostsymname - * conflicts with an existing persona. + * * -EINVAL if a required parameter is missing, if @hostnqn is given + * without @hostid and is not UUID-based, or if @hostsymname conflicts + * with an existing persona. * * -ENOMEM if memory allocation fails. */ int libnvmf_config_emit_add(struct libnvmf_config_emitter *emitter, diff --git a/libnvme/tests/config-emit.c b/libnvme/tests/config-emit.c index a97641b4b6..d6524fe8d4 100644 --- a/libnvme/tests/config-emit.c +++ b/libnvme/tests/config-emit.c @@ -528,6 +528,64 @@ static bool test_all_dropins(struct libnvme_global_ctx *ctx, struct fixture *fx) return pass; } +/* A hostnqn without a hostid takes the UUID of a UUID-based hostnqn. */ +static bool test_hostid_from_hostnqn(struct libnvme_global_ctx *ctx, + struct fixture *fx) +{ + struct libnvmf_config_emitter *e; + struct conn_list list = { 0 }; + struct libnvmf_config *config; + const char *hostid; + char hostnqn[96]; + bool pass = true; + + printf("test_hostid_from_hostnqn:\n"); + + snprintf(hostnqn, sizeof(hostnqn), + "nqn.2014-08.org.nvmexpress:uuid:%s", g_hostid); + + e = libnvmf_config_emit_new(ctx); + shr_assert(e); + shr_assert(add(e, false, "10.0.0.9", "nqn.2014-08.org.nvmexpress:a", + hostnqn, NULL, NULL, NULL, NULL) == 0); + if (libnvmf_config_emit_install(e, fx->main_path, false)) { + printf(" - install failed [FAIL]\n"); + libnvmf_config_emit_free(e); + return false; + } + libnvmf_config_emit_free(e); + + if (libnvmf_config_read(ctx, fx->main_path, &config)) { + printf(" - written config not readable [FAIL]\n"); + return false; + } + libnvmf_config_conn_for_each(config, collect, &list); + hostid = list.n == 1 ? + libnvmf_config_conn_get_hostid(list.conn[0]) : NULL; + if (!hostid || strcmp(hostid, g_hostid)) { + printf(" - hostid not taken from the hostnqn [FAIL]\n"); + pass = false; + } else { + printf(" - hostid taken from the hostnqn, read back [PASS]\n"); + } + libnvmf_config_free(config); + + /* No UUID in the hostnqn: nothing to take the hostid from. */ + e = libnvmf_config_emit_new(ctx); + shr_assert(e); + if (add(e, false, "10.0.0.9", "nqn.v", + "nqn.2014-08.org.nvmexpress:host-a", NULL, NULL, + NULL, NULL) != -EINVAL) { + printf(" - non-UUID hostnqn without hostid accepted [FAIL]\n"); + pass = false; + } else { + printf(" - non-UUID hostnqn without hostid rejected [PASS]\n"); + } + libnvmf_config_emit_free(e); + + return pass; +} + static bool test_add_validation(struct libnvme_global_ctx *ctx) { struct libnvmf_config_emitter *e; @@ -664,6 +722,8 @@ int main(void) fixture_wipe(&fx); pass &= test_all_dropins(ctx, &fx); fixture_wipe(&fx); + pass &= test_hostid_from_hostnqn(ctx, &fx); + fixture_wipe(&fx); pass &= test_add_validation(ctx); pass &= test_params_set_validation(); diff --git a/tests/cli/nvme_config_convert_test.py b/tests/cli/nvme_config_convert_test.py index 20e8d354df..d1e037ec86 100644 --- a/tests/cli/nvme_config_convert_test.py +++ b/tests/cli/nvme_config_convert_test.py @@ -151,6 +151,7 @@ def test_convert_dc_and_subsystem(self): def test_convert_legacy_bare_array_format(self): self._write_json([{ 'hostnqn': 'nqn.2014-08.org.nvmexpress:2222', + 'hostid': '22222222-2222-4222-8222-222222222222', 'subsystems': [{ 'nqn': 'nqn.2024-01.com.example:data.vol2', 'ports': [{ @@ -169,6 +170,7 @@ def test_dhchap_default_inherited_by_port(self): self._write_json({ 'hosts': [{ 'hostnqn': 'nqn.2014-08.org.nvmexpress:3333', + 'hostid': '33333333-3333-4333-8333-333333333333', 'dhchap_key': 'DHHC-1:00:host-default-key:', 'subsystems': [{ 'nqn': 'nqn.2024-01.com.example:data.vol3', @@ -187,6 +189,7 @@ def test_dhchap_port_value_overrides_default(self): self._write_json({ 'hosts': [{ 'hostnqn': 'nqn.2014-08.org.nvmexpress:4444', + 'hostid': '44444444-4444-4444-8444-444444444444', 'dhchap_key': 'DHHC-1:00:host-default-key:', 'subsystems': [{ 'nqn': 'nqn.2024-01.com.example:data.vol4', @@ -301,6 +304,23 @@ def test_malformed_json_errors(self): self._convert(expect_fail=True) self.assertFalse(os.path.exists(self.output_ini)) + def test_hostnqn_without_hostid_is_skipped(self): + self._write_json({ + 'hosts': [{ + 'hostnqn': 'nqn.2014-08.org.nvmexpress:5555', + 'subsystems': [{ + 'nqn': 'nqn.2024-01.com.example:data.vol5', + 'ports': [{ + 'transport': 'tcp', + 'traddr': '10.0.0.8', + }], + }], + }], + }) + result = self._convert() + self.assertIn('requires a hostid', result.stderr) + self.assertNotIn('data.vol5', self._read_output()) + def test_missing_hosts_array_errors(self): self._write_json({'not-hosts': []}) self._convert(expect_fail=True) diff --git a/tests/cli/nvme_config_create_test.py b/tests/cli/nvme_config_create_test.py index 979d0af20d..c970a236ba 100644 --- a/tests/cli/nvme_config_create_test.py +++ b/tests/cli/nvme_config_create_test.py @@ -115,10 +115,23 @@ def test_host_symname_gets_own_dropin(self): self._create('--transport', 'tcp', '--traddr=192.168.1.21', '--nqn=nqn.2024-01.com.example:data.vol2', '--hostnqn=nqn.2014-08.org.nvmexpress:aaaa', + '--hostid=46ba5037-7ce5-41fa-9452-48477bf00080', '--host-symname=lab-host-01') content = self._read_output() self.assertIn('hostsymname = lab-host-01', content) + def test_hostid_taken_from_uuid_hostnqn(self): + uuid = '46ba5037-7ce5-41fa-9452-48477bf00080' + args = ('--transport', 'tcp', '--traddr=192.168.1.22', + f'--hostnqn=nqn.2014-08.org.nvmexpress:uuid:{uuid}') + self._create(*args, '--nqn=nqn.2024-01.com.example:data.vol7') + self.assertIn(f'hostid = {uuid}', self._read_output()) + # A second create reads the first file back. + self._create(*args, '--nqn=nqn.2024-01.com.example:data.vol8') + content = self._read_output() + self.assertIn('nqn = nqn.2024-01.com.example:data.vol7', content) + self.assertIn('nqn = nqn.2024-01.com.example:data.vol8', content) + def test_discovery_persistent_bare_defaults_to_auto(self): self._create('--transport', 'fc', '--traddr=nn-0x2:pn-0x2', '--discovery', @@ -216,6 +229,13 @@ def test_io_controller_without_nqn_errors(self): expect_fail=True) self.assertFalse(os.path.exists(self.output_ini)) + def test_hostnqn_without_hostid_errors(self): + self._create('--transport', 'tcp', '--traddr=192.168.1.44', + '--nqn=nqn.2024-01.com.example:data.vol9', + '--hostnqn=nqn.2014-08.org.nvmexpress:aaaa', + expect_fail=True) + self.assertFalse(os.path.exists(self.output_ini)) + def test_missing_traddr_errors(self): result = self._create('--transport', 'tcp', '--discovery', expect_fail=True) From ef395860a846cfa872d2d3de3407f3fd00d20c72 Mon Sep 17 00:00:00 2001 From: Martin Belanger Date: Mon, 30 Nov 2026 07:04:26 -0500 Subject: [PATCH 2/3] libnvme: keep existing files when a config install fails On failure, libnvmf_config_emit_install() removed every destination file, also files it had not replaced yet. "nvme config create" always installs with force, so a failed write (ENOSPC, EIO) deleted drop-ins of a working configuration. Remove only the temporary files. Fixes: b5f6b1579609 ("libnvme/config: add the configuration write side") Signed-off-by: Martin Belanger --- libnvme/src/nvme/config-emit.c | 15 +++----- libnvme/src/nvme/config.h | 8 +++-- libnvme/tests/config-emit.c | 64 ++++++++++++++++++++++++++++++++++ 3 files changed, 74 insertions(+), 13 deletions(-) diff --git a/libnvme/src/nvme/config-emit.c b/libnvme/src/nvme/config-emit.c index c219b239e2..a1faf3ba26 100644 --- a/libnvme/src/nvme/config-emit.c +++ b/libnvme/src/nvme/config-emit.c @@ -474,17 +474,12 @@ static void outfiles_free(struct outfile *out, size_t n, bool rollback) size_t i; for (i = 0; i < n; i++) { - if (rollback) { - /* - * Roll back a partial install: each entry was either - * already renamed into place (only @final exists) or - * never got that far (only @tmp exists). Removing - * both is safe -- whichever one isn't there just - * fails with a harmless ENOENT. - */ - unlink(out[i].final); + /* + * Remove only the temp files. A final path may hold a file + * from before this install, which must not be lost. + */ + if (rollback) unlink(out[i].tmp); - } free(out[i].tmp); free(out[i].final); } diff --git a/libnvme/src/nvme/config.h b/libnvme/src/nvme/config.h index 4c76b4c88a..78786a0887 100644 --- a/libnvme/src/nvme/config.h +++ b/libnvme/src/nvme/config.h @@ -483,9 +483,11 @@ int libnvmf_config_emit_add(struct libnvmf_config_emitter *emitter, * named persona is written to a separate drop-in file. * * The generated configuration is validated by reading it back through the - * standard parser before it is installed. Files are written atomically - * (temporary file followed by rename). If any step fails, no configuration - * is installed. + * standard parser before it is installed. Each file is written to a + * temporary file, and all of them are renamed into place only after every + * temporary file was written. If writing fails, existing files are left + * unchanged. If a rename fails, the files renamed before it hold the new + * configuration. * * Return: * * 0 on success. diff --git a/libnvme/tests/config-emit.c b/libnvme/tests/config-emit.c index d6524fe8d4..14b04d9be2 100644 --- a/libnvme/tests/config-emit.c +++ b/libnvme/tests/config-emit.c @@ -416,6 +416,68 @@ static bool test_force_overwrite(struct libnvme_global_ctx *ctx, return pass; } +/* + * A failed force install keeps the files it did not replace. The main + * file's name leaves no room for the temp-file suffix: its temp file + * cannot be created, after the drop-in's temp file was. + */ +static bool test_force_failure_keeps_files(struct libnvme_global_ctx *ctx, + struct fixture *fx) +{ + char name[251], main_path[300], ddir[304], dropin[324], buf[16]; + struct libnvmf_config_emitter *e; + bool pass = true; + FILE *f; + + printf("test_force_failure_keeps_files:\n"); + + memset(name, 'c', sizeof(name) - 1); + name[sizeof(name) - 1] = '\0'; + snprintf(main_path, sizeof(main_path), "%s/%s", fx->dir, name); + snprintf(ddir, sizeof(ddir), "%s.d", main_path); + snprintf(dropin, sizeof(dropin), "%s/100-persona.conf", ddir); + + /* An existing configuration: a main file and one drop-in. */ + shr_assert(mkdir(ddir, 0755) == 0); + f = fopen(main_path, "w"); + shr_assert(f); + shr_assert(fclose(f) == 0); + f = fopen(dropin, "w"); + shr_assert(f); + shr_assert(fputs("old\n", f) >= 0); + shr_assert(fclose(f) == 0); + + e = libnvmf_config_emit_new(ctx); + shr_assert(e); + shr_assert(add(e, false, "10.0.0.9", "nqn.2014-08.org.nvmexpress:a", + "nqn.2014-08.org.nvmexpress:host-a", g_hostid, NULL, + NULL, NULL) == 0); + if (!libnvmf_config_emit_install(e, main_path, true)) { + printf(" - install did not fail [FAIL]\n"); + pass = false; + } + libnvmf_config_emit_free(e); + + f = fopen(dropin, "r"); + if (!f || !fgets(buf, sizeof(buf), f) || strcmp(buf, "old\n")) { + printf(" - existing drop-in lost or changed [FAIL]\n"); + pass = false; + } else if (access(main_path, F_OK)) { + printf(" - existing main file lost [FAIL]\n"); + pass = false; + } else { + printf(" - failed install keeps existing files [PASS]\n"); + } + if (f) + fclose(f); + + unlink(dropin); + rmdir(ddir); + unlink(main_path); + + return pass; +} + static bool install_one(struct libnvme_global_ctx *ctx, const char *path, bool force) { @@ -718,6 +780,8 @@ int main(void) fixture_wipe(&fx); pass &= test_force_overwrite(ctx, &fx); fixture_wipe(&fx); + pass &= test_force_failure_keeps_files(ctx, &fx); + fixture_wipe(&fx); pass &= test_file_mode(ctx, &fx); fixture_wipe(&fx); pass &= test_all_dropins(ctx, &fx); From 12607b89d3c4e94a76c1a9c58f9af7cf78da0992 Mon Sep 17 00:00:00 2001 From: Martin Belanger Date: Mon, 30 Nov 2026 12:47:32 -0500 Subject: [PATCH 3/3] libnvme: drop the static copy of hostid_from_hostnqn() libnvmf_context_set_hostnqn() had its own copy of libnvme_hostid_from_hostnqn(). Use the shared function, and remove its duplicate declaration from private-tree.h. Signed-off-by: Martin Belanger --- libnvme/src/nvme/fabrics.c | 36 ++++++++++----------------------- libnvme/src/nvme/private-tree.h | 2 -- libnvme/tests/test-fabrics.c | 18 ++++++++--------- 3 files changed, 20 insertions(+), 36 deletions(-) diff --git a/libnvme/src/nvme/fabrics.c b/libnvme/src/nvme/fabrics.c index 74957c354e..d39f5d4e5e 100644 --- a/libnvme/src/nvme/fabrics.c +++ b/libnvme/src/nvme/fabrics.c @@ -841,20 +841,6 @@ __shr_public int libnvmf_context_set_connection( return 0; } -static const char *hostid_from_hostnqn(const char *hostnqn) -{ - const char *match; - - if (!hostnqn) - return NULL; - - match = strstr(hostnqn, "uuid:"); - if (!match) - return NULL; - - return match + strlen("uuid:"); -} - __shr_public int libnvmf_context_set_hostnqn(struct libnvmf_context *fctx, const char *hostnqn, const char *hostid) { @@ -868,18 +854,18 @@ __shr_public int libnvmf_context_set_hostnqn(struct libnvmf_context *fctx, if (!hnqn) return -ENOMEM; - if (!hostid) - hostid = hostid_from_hostnqn(hostnqn); - if (!hostid) { - free(hnqn); - return -EINVAL; - } - - hid = strdup(hostid); - if (!hid) { - free(hnqn); - return -ENOMEM; + hid = libnvme_hostid_from_hostnqn(hostnqn); + if (!hid) { + free(hnqn); + return -EINVAL; + } + } else { + hid = strdup(hostid); + if (!hid) { + free(hnqn); + return -ENOMEM; + } } free(fctx->hostnqn); diff --git a/libnvme/src/nvme/private-tree.h b/libnvme/src/nvme/private-tree.h index 6beadffa94..83499b6443 100644 --- a/libnvme/src/nvme/private-tree.h +++ b/libnvme/src/nvme/private-tree.h @@ -39,8 +39,6 @@ extern char NO_ATTR[]; "nqn.2014-08.org.nvmexpress:uuid:00000000-0000-0000-0000-000000000000" #define NVME_DEFAULT_HOSTID "00000000-0000-0000-0000-000000000000" -char *libnvme_hostid_from_hostnqn(const char *hostnqn); - /** * libnvme_parse_src_addr - Extract the source address from an address string * @ctx: libnvme global context diff --git a/libnvme/tests/test-fabrics.c b/libnvme/tests/test-fabrics.c index 3b6f00e4cc..fc78ec241e 100644 --- a/libnvme/tests/test-fabrics.c +++ b/libnvme/tests/test-fabrics.c @@ -75,32 +75,32 @@ static bool test_sanitize_discovery_log_entry(struct libnvme_global_ctx *ctx) } /* ------------------------------------------------------------------------- - * hostid_from_hostnqn — extract UUID from a hostnqn string + * libnvme_hostid_from_hostnqn — extract UUID from a hostnqn string * ------------------------------------------------------------------------- */ static bool test_hostid_from_hostnqn(void) { - const char *id; + char *id; bool pass = true; printf("\ntest_hostid_from_hostnqn:\n"); - id = hostid_from_hostnqn(NULL); - pass = (id == NULL); - CHECK(pass, "NULL input → NULL"); - - id = hostid_from_hostnqn("nqn.2014-08.org.nvmexpress:no-uuid-here"); + id = libnvme_hostid_from_hostnqn( + "nqn.2014-08.org.nvmexpress:no-uuid-here"); pass = (id == NULL); CHECK(pass, "no uuid: tag → NULL"); + free(id); - id = hostid_from_hostnqn( + id = libnvme_hostid_from_hostnqn( "nqn.2014-08.org.nvmexpress:uuid:9ba1651a-ed36-11f0-9858-6c1ff71ba506"); pass = (id && !strcmp(id, "9ba1651a-ed36-11f0-9858-6c1ff71ba506")); CHECK(pass, "valid NQN → UUID \"%s\"", id ? id : "(null)"); + free(id); - id = hostid_from_hostnqn("prefix:uuid:abc-123"); + id = libnvme_hostid_from_hostnqn("prefix:uuid:abc-123"); pass = (id && !strcmp(id, "abc-123")); CHECK(pass, "arbitrary prefix → \"abc-123\""); + free(id); return pass; }