Skip to content

Commit

Permalink
fabrics: Always pass hostid and hostnqn
Browse files Browse the repository at this point in the history
After the kernel commit ae8bd606e09b ("nvme-fabrics: prevent overriding
of existing host"), kernel ensures hostid and hostnqn maintain 1:1
mapping. This makes 'nvme discover' and 'nvme connect' commands fail
when providing only hostid or only hostnqn. This issue happens when
the user only enters NQN which doesn't contain UUID, so the generation
of the hostid fails.

There are few more issues that this commit is fixing:
 - When the user provides hostid and NQN, the hostid is overridden
   by generating it from the NQN.
 - hostid is generated from the NQN file instead of the NQN that
   the user enters at the command line.
 - The warning "use generated hostid instead of hostid file" is
   wrong when the user provides hostid via the command line.

The commit fixes those issues by doing the following logic:
 1. If user provided both via command line - pass them as-is
 2. If user doesn't enter them via command line - try to get
    them from files.
 3. If one of them is not provided - generate it from the other.
    Use the new functions nvmf_hostid_generate() when NQN doesn't
    have UUID and use nvmf_hostnqn_generate_from_hostid(hostid) to
    generate hostnqn from hostid.
 4. If user provided none - generate them both. Before this commit,
    nvme cli didn't do it.

Signed-off-by: Israel Rukshin <[email protected]>
Reviewed-by: Max Gurtovoy <[email protected]>
Signed-off-by: Daniel Wagner <[email protected]>
  • Loading branch information
israelru authored and igaw committed May 22, 2024
1 parent ab398f5 commit 0420a39
Showing 1 changed file with 46 additions and 32 deletions.
78 changes: 46 additions & 32 deletions fabrics.c
Original file line number Diff line number Diff line change
Expand Up @@ -643,20 +643,9 @@ char *nvmf_hostid_from_hostnqn(const char *hostnqn)

void nvmf_check_hostid_and_hostnqn(const char *hostid, const char *hostnqn, unsigned int verbose)
{
_cleanup_free_ char *hostid_from_file = NULL;
_cleanup_free_ char *hostid_from_hostnqn = NULL;

if (!hostid)
return;

hostid_from_file = nvmf_hostid_from_file();
if (hostid_from_file && strcmp(hostid_from_file, hostid)) {
if (verbose)
fprintf(stderr,
"warning: use generated hostid instead of hostid file\n");
}

if (!hostnqn)
if (!hostnqn || !hostid)
return;

hostid_from_hostnqn = nvmf_hostid_from_hostnqn(hostnqn);
Expand All @@ -667,6 +656,34 @@ void nvmf_check_hostid_and_hostnqn(const char *hostid, const char *hostnqn, unsi
}
}

void nvmf_set_hostid_and_hostnqn(char **hostid, char **hostnqn)
{
char *hid = *hostid;
char *hnqn = *hostnqn;

if (!hid)
hid = nvmf_hostid_from_file();
if (!hnqn)
hnqn = nvmf_hostnqn_from_file();

if (!hid) {
if (hnqn) {
hid = nvmf_hostid_from_hostnqn(hnqn);
if (!hid)
hid = nvmf_hostid_generate();
} else {
hid = nvmf_hostid_generate();
hnqn = nvmf_hostnqn_generate_from_hostid(hid);
}
}

if (!hnqn)
hnqn = nvmf_hostnqn_generate_from_hostid(hid);

*hostid = hid;
*hostnqn = hnqn;
}

int nvmf_discover(const char *desc, int argc, char **argv, bool connect)
{
char *subsysnqn = NVME_DISC_SUBSYS_NAME;
Expand Down Expand Up @@ -746,16 +763,13 @@ int nvmf_discover(const char *desc, int argc, char **argv, bool connect)

hostnqn_arg = hostnqn;
hostid_arg = hostid;
if (!hostnqn)
hostnqn = hnqn = nvmf_hostnqn_from_file();
if (!hostnqn) {
hostnqn = hnqn = nvmf_hostnqn_generate();
hostid = hid = nvmf_hostid_from_hostnqn(hostnqn);
}
if (!hostid)
hostid = hid = nvmf_hostid_from_file();
if (!hostid && hostnqn)
hostid = hid = nvmf_hostid_from_hostnqn(hostnqn);

nvmf_set_hostid_and_hostnqn(&hostid, &hostnqn);
if (!hostid_arg)
hid = hostid;
if (!hostnqn_arg)
hnqn = hostnqn;

nvmf_check_hostid_and_hostnqn(hostid, hostnqn, verbose);
h = nvme_lookup_host(r, hostnqn, hostid);
if (!h) {
Expand Down Expand Up @@ -905,6 +919,7 @@ int nvmf_connect(const char *desc, int argc, char **argv)
enum nvme_print_flags flags;
struct nvme_fabrics_config cfg = { 0 };
char *format = "normal";
char *hostnqn_arg, *hostid_arg;


NVMF_ARGS(opts, cfg,
Expand Down Expand Up @@ -972,16 +987,15 @@ int nvmf_connect(const char *desc, int argc, char **argv)
nvme_read_config(r, config_file);
nvme_read_volatile_config(r);

if (!hostnqn)
hostnqn = hnqn = nvmf_hostnqn_from_file();
if (!hostnqn) {
hostnqn = hnqn = nvmf_hostnqn_generate();
hostid = hid = nvmf_hostid_from_hostnqn(hostnqn);
}
if (!hostid)
hostid = hid = nvmf_hostid_from_file();
if (!hostid && hostnqn)
hostid = hid = nvmf_hostid_from_hostnqn(hostnqn);
hostnqn_arg = hostnqn;
hostid_arg = hostid;

nvmf_set_hostid_and_hostnqn(&hostid, &hostnqn);
if (!hostid_arg)
hid = hostid;
if (!hostnqn_arg)
hnqn = hostnqn;

nvmf_check_hostid_and_hostnqn(hostid, hostnqn, verbose);
h = nvme_lookup_host(r, hostnqn, hostid);
if (!h) {
Expand Down

0 comments on commit 0420a39

Please sign in to comment.