Skip to content

Commit 2a7c1bb

Browse files
Martin Belangerigaw
authored andcommitted
plugins/exclusion: operate on the default list when --name is omitted
The library treats a NULL list name as the default list (/etc/nvme/exclusions.conf) -- the list nvme disconnect --exclude writes to -- but every mutating exclusion subcommand rejected a missing --name, so the CLI could not manage the most commonly written list at all. Make --name optional on create, delete, add, remove and edit; omitting it targets the default list. Also stop reporting a bad list name as "invalid entry": validate the entry first and attribute a remaining -EINVAL to the name. Add unit tests covering the NULL-name (default list) operations. Signed-off-by: Martin Belanger <martin.belanger@dell.com>
1 parent a4217c8 commit 2a7c1bb

7 files changed

Lines changed: 96 additions & 38 deletions

File tree

Documentation/nvme-exclusion-add.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ nvme-exclusion-add - Add an entry to an NVMe-oF exclusion list
88
SYNOPSIS
99
--------
1010
[verse]
11-
'nvme' [<global-options>] 'exclusion add' --name=<NAME> | -N <NAME>
11+
'nvme' [<global-options>] 'exclusion add' [--name=<NAME> | -N <NAME>]
1212
--entry=<ENTRY> | -e <ENTRY>
1313

1414
DESCRIPTION
@@ -32,7 +32,8 @@ OPTIONS
3232
-------
3333
-N <NAME>::
3434
--name=<NAME>::
35-
Name of the exclusion list to add to.
35+
Name of the exclusion list to add to. Omit to add to the
36+
default list (/etc/nvme/exclusions.conf).
3637

3738
-e <ENTRY>::
3839
--entry=<ENTRY>::

Documentation/nvme-exclusion-create.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ nvme-exclusion-create - Create a new NVMe-oF exclusion list
88
SYNOPSIS
99
--------
1010
[verse]
11-
'nvme' [<global-options>] 'exclusion create' --name=<NAME> | -N <NAME>
11+
'nvme' [<global-options>] 'exclusion create' [--name=<NAME> | -N <NAME>]
1212

1313
DESCRIPTION
1414
-----------
@@ -24,7 +24,8 @@ OPTIONS
2424
-------
2525
-N <NAME>::
2626
--name=<NAME>::
27-
Name of the exclusion list to create.
27+
Name of the exclusion list to create. Omit to create the
28+
default list (/etc/nvme/exclusions.conf).
2829

2930
include::global-options.txt[]
3031

Documentation/nvme-exclusion-delete.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ nvme-exclusion-delete - Delete an NVMe-oF exclusion list
88
SYNOPSIS
99
--------
1010
[verse]
11-
'nvme' [<global-options>] 'exclusion delete' --name=<NAME> | -N <NAME>
11+
'nvme' [<global-options>] 'exclusion delete' [--name=<NAME> | -N <NAME>]
1212

1313
DESCRIPTION
1414
-----------
@@ -22,7 +22,8 @@ OPTIONS
2222
-------
2323
-N <NAME>::
2424
--name=<NAME>::
25-
Name of the exclusion list to delete.
25+
Name of the exclusion list to delete. Omit to delete the
26+
default list (/etc/nvme/exclusions.conf).
2627

2728
include::global-options.txt[]
2829

Documentation/nvme-exclusion-edit.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ nvme-exclusion-edit - Edit an NVMe-oF exclusion list in an editor
88
SYNOPSIS
99
--------
1010
[verse]
11-
'nvme' [<global-options>] 'exclusion edit' --name=<NAME> | -N <NAME>
11+
'nvme' [<global-options>] 'exclusion edit' [--name=<NAME> | -N <NAME>]
1212

1313
DESCRIPTION
1414
-----------
@@ -30,7 +30,8 @@ OPTIONS
3030
-------
3131
-N <NAME>::
3232
--name=<NAME>::
33-
Name of the exclusion list to edit.
33+
Name of the exclusion list to edit. Omit to edit the default
34+
list (/etc/nvme/exclusions.conf).
3435

3536
include::global-options.txt[]
3637

Documentation/nvme-exclusion-remove.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ nvme-exclusion-remove - Remove an entry from an NVMe-oF exclusion list
88
SYNOPSIS
99
--------
1010
[verse]
11-
'nvme' [<global-options>] 'exclusion remove' --name=<NAME> | -N <NAME>
11+
'nvme' [<global-options>] 'exclusion remove' [--name=<NAME> | -N <NAME>]
1212

1313
DESCRIPTION
1414
-----------
@@ -23,7 +23,8 @@ OPTIONS
2323
-------
2424
-N <NAME>::
2525
--name=<NAME>::
26-
Name of the exclusion list to remove an entry from.
26+
Name of the exclusion list to remove an entry from. Omit to
27+
remove from the default list (/etc/nvme/exclusions.conf).
2728

2829
include::global-options.txt[]
2930

libnvme/test/exclusion.c

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,73 @@ static bool test_section_semantics(struct libnvme_global_ctx *ctx)
442442
return pass;
443443
}
444444

445+
446+
/*
447+
* The default (main) list: name == NULL addresses exclusions.conf itself.
448+
* This is the path "nvme exclusion <cmd>" without --name and
449+
* "nvme disconnect --exclude" rely on.
450+
*/
451+
static bool test_default_list(struct libnvme_global_ctx *ctx)
452+
{
453+
char path[512];
454+
struct libnvmf_tid *tid;
455+
struct stat st;
456+
bool pass = true;
457+
int ret;
458+
459+
printf("test_default_list:\n");
460+
461+
/* create(NULL) makes the main file at 0644 with the header. */
462+
ret = libnvmf_exclusion_create(ctx, NULL);
463+
snprintf(path, sizeof(path), "%s/exclusions.conf", tmpdir);
464+
if (ret || stat(path, &st) < 0 || (st.st_mode & 07777) != 0644) {
465+
printf(" - create(NULL) ret=%d [FAIL]\n", ret);
466+
pass = false;
467+
} else {
468+
printf(" - create(NULL) -> exclusions.conf at 0644 [PASS]\n");
469+
}
470+
471+
ret = libnvmf_exclusion_add(ctx, NULL, "transport=tcp;traddr=7.7.7.7");
472+
if (ret || entry_count(ctx, NULL) != 1) {
473+
printf(" - add(NULL) ret=%d count=%d [FAIL]\n",
474+
ret, entry_count(ctx, NULL));
475+
pass = false;
476+
} else {
477+
printf(" - add(NULL) -> 1 entry in the default list [PASS]\n");
478+
}
479+
480+
tid = libnvmf_tid_from_fields("tcp", "7.7.7.7", "4420", "nqn.x",
481+
NULL, NULL, NULL, NULL);
482+
if (libnvmf_exclusion_match(ctx, tid)) {
483+
printf(" - default-list entry matches [PASS]\n");
484+
} else {
485+
printf(" - default-list entry matches [FAIL]\n");
486+
pass = false;
487+
}
488+
libnvmf_tid_free(tid);
489+
490+
ret = libnvmf_exclusion_remove(ctx, NULL,
491+
"transport=tcp;traddr=7.7.7.7");
492+
if (ret || entry_count(ctx, NULL) != 0) {
493+
printf(" - remove(NULL) ret=%d count=%d [FAIL]\n",
494+
ret, entry_count(ctx, NULL));
495+
pass = false;
496+
} else {
497+
printf(" - remove(NULL) -> default list empty again [PASS]\n");
498+
}
499+
500+
/* delete(NULL) removes the main file itself. */
501+
ret = libnvmf_exclusion_delete(ctx, NULL);
502+
if (ret || stat(path, &st) == 0) {
503+
printf(" - delete(NULL) ret=%d [FAIL]\n", ret);
504+
pass = false;
505+
} else {
506+
printf(" - delete(NULL) removes exclusions.conf [PASS]\n");
507+
}
508+
509+
return pass;
510+
}
511+
445512
int main(void)
446513
{
447514
struct libnvme_global_ctx *ctx;
@@ -458,6 +525,7 @@ int main(void)
458525
pass &= test_invalid_entry_rejected(ctx);
459526
pass &= test_missing_then_create(ctx);
460527
pass &= test_section_semantics(ctx);
528+
pass &= test_default_list(ctx);
461529
pass &= test_null_args(ctx);
462530

463531
cleanup_tmpdir(ctx);

plugins/exclusion/exclusion-nvme.c

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ static int excl_create(int argc, char **argv, struct command *acmd,
4343
struct plugin *plugin)
4444
{
4545
const char *desc = "Create a new NVMeoF exclusion list.";
46-
const char *name_help = "exclusion list name";
46+
const char *name_help = "exclusion list name (omit for the default list)";
4747
__cleanup_nvme_global_ctx struct libnvme_global_ctx *ctx = NULL;
4848
int ret;
4949

@@ -58,11 +58,6 @@ static int excl_create(int argc, char **argv, struct command *acmd,
5858
if (ret)
5959
return ret;
6060

61-
if (!cfg.name) {
62-
fprintf(stderr, "--name required\n");
63-
return -EINVAL;
64-
}
65-
6661
ret = require_root();
6762
if (ret)
6863
return ret;
@@ -80,7 +75,7 @@ static int excl_delete(int argc, char **argv, struct command *acmd,
8075
struct plugin *plugin)
8176
{
8277
const char *desc = "Delete an NVMeoF exclusion list entirely.";
83-
const char *name_help = "exclusion list name";
78+
const char *name_help = "exclusion list name (omit for the default list)";
8479
__cleanup_nvme_global_ctx struct libnvme_global_ctx *ctx = NULL;
8580
int ret;
8681

@@ -95,11 +90,6 @@ static int excl_delete(int argc, char **argv, struct command *acmd,
9590
if (ret)
9691
return ret;
9792

98-
if (!cfg.name) {
99-
fprintf(stderr, "--name required\n");
100-
return -EINVAL;
101-
}
102-
10393
ret = require_root();
10494
if (ret)
10595
return ret;
@@ -163,7 +153,7 @@ static int excl_add(int argc, char **argv, struct command *acmd,
163153
struct plugin *plugin)
164154
{
165155
const char *desc = "Add an entry to an NVMeoF exclusion list.";
166-
const char *name_help = "exclusion list name";
156+
const char *name_help = "exclusion list name (omit for the default list)";
167157
const char *entry_help = "semicolon-separated key=value entry "
168158
"(e.g. \"transport=tcp;traddr=192.168.1.1\")";
169159
__cleanup_nvme_global_ctx struct libnvme_global_ctx *ctx = NULL;
@@ -182,8 +172,8 @@ static int excl_add(int argc, char **argv, struct command *acmd,
182172
if (ret)
183173
return ret;
184174

185-
if (!cfg.name || !cfg.entry) {
186-
fprintf(stderr, "--name and --entry required\n");
175+
if (!cfg.entry) {
176+
fprintf(stderr, "--entry required\n");
187177
return -EINVAL;
188178
}
189179

@@ -192,9 +182,14 @@ static int excl_add(int argc, char **argv, struct command *acmd,
192182
return ret;
193183

194184
ctx = libnvme_create_global_ctx();
185+
if (!libnvmf_exclusion_entry_valid(ctx, cfg.entry)) {
186+
fprintf(stderr, "invalid entry: %s\n", cfg.entry);
187+
return -EINVAL;
188+
}
189+
195190
ret = libnvmf_exclusion_add(ctx, cfg.name, cfg.entry);
196191
if (ret == -EINVAL)
197-
fprintf(stderr, "invalid entry (unknown key): %s\n", cfg.entry);
192+
fprintf(stderr, "invalid list name: %s\n", cfg.name);
198193
else if (ret)
199194
fprintf(stderr, "add failed: %s\n", libnvme_strerror(-ret));
200195
return ret;
@@ -232,7 +227,7 @@ static int excl_remove(int argc, char **argv, struct command *acmd,
232227
struct plugin *plugin)
233228
{
234229
const char *desc = "Interactively remove an entry from an NVMeoF exclusion list.";
235-
const char *name_help = "exclusion list name";
230+
const char *name_help = "exclusion list name (omit for the default list)";
236231
__cleanup_nvme_global_ctx struct libnvme_global_ctx *ctx = NULL;
237232
struct entry_collection ec = { 0 };
238233
char answer[32];
@@ -250,11 +245,6 @@ static int excl_remove(int argc, char **argv, struct command *acmd,
250245
if (ret)
251246
return ret;
252247

253-
if (!cfg.name) {
254-
fprintf(stderr, "--name required\n");
255-
return -EINVAL;
256-
}
257-
258248
ret = require_root();
259249
if (ret)
260250
return ret;
@@ -490,7 +480,7 @@ static int excl_edit(int argc, char **argv, struct command *acmd,
490480
struct plugin *plugin)
491481
{
492482
const char *desc = "Interactively edit an NVMeoF exclusion list.";
493-
const char *name_help = "exclusion list name";
483+
const char *name_help = "exclusion list name (omit for the default list)";
494484
__cleanup_nvme_global_ctx struct libnvme_global_ctx *ctx = NULL;
495485
__cleanup_free char *text = NULL;
496486
const char *tmpdir;
@@ -509,11 +499,6 @@ static int excl_edit(int argc, char **argv, struct command *acmd,
509499
if (ret)
510500
return ret;
511501

512-
if (!cfg.name) {
513-
fprintf(stderr, "--name required\n");
514-
return -EINVAL;
515-
}
516-
517502
ret = require_root();
518503
if (ret)
519504
return ret;

0 commit comments

Comments
 (0)