From a3bac573c955fec12942b4bfa5d10b68931210cb Mon Sep 17 00:00:00 2001 From: Hans Rosenfeld Date: Tue, 22 Mar 2022 17:47:27 +0100 Subject: 14670 nvmeadm refactor for option parsing Reviewed by: Andrew Giles Reviewed by: Ben Jameson Reviewed by: Guy Morrogh Reviewed by: Robert Mustacchi Approved by: Dan McDonald --- usr/src/cmd/nvmeadm/nvmeadm.c | 57 +++++++++++++++++++++++++++++++++---------- usr/src/cmd/nvmeadm/nvmeadm.h | 1 + usr/src/man/man8/nvmeadm.8 | 6 ++--- 3 files changed, 48 insertions(+), 16 deletions(-) diff --git a/usr/src/cmd/nvmeadm/nvmeadm.c b/usr/src/cmd/nvmeadm/nvmeadm.c index d78c7fe9ee..399631da88 100644 --- a/usr/src/cmd/nvmeadm/nvmeadm.c +++ b/usr/src/cmd/nvmeadm/nvmeadm.c @@ -103,6 +103,7 @@ static int do_firmware_commit(int, const nvme_process_arg_t *); static int do_firmware_activate(int, const nvme_process_arg_t *); static void optparse_list(nvme_process_arg_t *); +static void optparse_secure_erase(nvme_process_arg_t *); static void usage_list(const char *); static void usage_identify(const char *); @@ -118,6 +119,9 @@ static void usage_firmware_activate(const char *); int verbose; int debug; + +#define NVMEADM_O_SE_CRYPTO 0x00000004 + static int exitcode; static const nvmeadm_cmd_t nvmeadm_cmds[] = { @@ -161,7 +165,7 @@ static const nvmeadm_cmd_t nvmeadm_cmds[] = { "secure-erase", "secure erase namespace(s) of a controller", " -c Do a cryptographic erase.", - do_secure_erase, usage_secure_erase, NULL, + do_secure_erase, usage_secure_erase, optparse_secure_erase, NVMEADM_C_EXCL }, { @@ -406,10 +410,12 @@ nvme_oferr(const char *fmt, ...) static void usage(const nvmeadm_cmd_t *cmd) { + const char *progname = getprogname(); + (void) fprintf(stderr, "usage:\n"); - (void) fprintf(stderr, " %s -h %s\n", getprogname(), + (void) fprintf(stderr, " %s -h %s\n", progname, cmd != NULL ? cmd->c_name : "[]"); - (void) fprintf(stderr, " %s [-dv] ", getprogname()); + (void) fprintf(stderr, " %s [-dv] ", progname); if (cmd != NULL) { cmd->c_usage(cmd->c_name); @@ -424,12 +430,17 @@ usage(const nvmeadm_cmd_t *cmd) (void) fprintf(stderr, " %-18s - %s\n", cmd->c_name, cmd->c_desc); } - (void) fprintf(stderr, "\nflags:\n" + (void) fprintf(stderr, "\n%s flags:\n" " -h\t\tprint usage information\n" " -d\t\tprint information useful for debugging %s\n" - " -v\t\tprint verbose information\n", getprogname()); - if (cmd != NULL && cmd->c_flagdesc != NULL) + " -v\t\tprint verbose information\n", + progname, progname); + + if (cmd != NULL && cmd->c_flagdesc != NULL) { + (void) fprintf(stderr, "\n%s %s flags:\n", + progname, cmd->c_name); (void) fprintf(stderr, "%s\n", cmd->c_flagdesc); + } } static boolean_t @@ -1216,17 +1227,41 @@ do_format(int fd, const nvme_process_arg_t *npa) static void usage_secure_erase(const char *c_name) { - (void) fprintf(stderr, "%s [/] [-c]\n\n" + (void) fprintf(stderr, "%s [-c] [/]\n\n" " Secure-Erase one or all namespaces of the specified " "NVMe controller.\n", c_name); } +static void +optparse_secure_erase(nvme_process_arg_t *npa) +{ + int c; + + optind = 0; + while ((c = getopt(npa->npa_argc, npa->npa_argv, ":c")) != -1) { + switch (c) { + case 'c': + npa->npa_cmdflags |= NVMEADM_O_SE_CRYPTO; + break; + case '?': + errx(-1, "unknown secure-erase option: -%c", optopt); + break; + } + } + + npa->npa_argc -= optind; + npa->npa_argv += optind; +} + static int do_secure_erase(int fd, const nvme_process_arg_t *npa) { unsigned long lbaf; uint8_t ses = NVME_FRMT_SES_USER; + if (npa->npa_argc > 0) + errx(-1, "Too many arguments"); + if (npa->npa_idctl->id_oacs.oa_format == 0) errx(-1, "%s not supported", npa->npa_cmd->c_name); @@ -1234,12 +1269,8 @@ do_secure_erase(int fd, const nvme_process_arg_t *npa) errx(-1, "%s not supported on individual namespace", npa->npa_cmd->c_name); - if (npa->npa_argc > 0) { - if (strcmp(npa->npa_argv[0], "-c") == 0) - ses = NVME_FRMT_SES_CRYPTO; - else - usage(npa->npa_cmd); - } + if ((npa->npa_cmdflags & NVMEADM_O_SE_CRYPTO) != 0) + ses = NVME_FRMT_SES_CRYPTO; if (ses == NVME_FRMT_SES_CRYPTO && npa->npa_idctl->id_fna.fn_crypt_erase == 0) diff --git a/usr/src/cmd/nvmeadm/nvmeadm.h b/usr/src/cmd/nvmeadm/nvmeadm.h index e6c16a8252..ab4d4e4113 100644 --- a/usr/src/cmd/nvmeadm/nvmeadm.h +++ b/usr/src/cmd/nvmeadm/nvmeadm.h @@ -45,6 +45,7 @@ struct nvme_process_arg { boolean_t npa_isns; boolean_t npa_ignored; boolean_t npa_interactive; + uint32_t npa_cmdflags; const nvmeadm_cmd_t *npa_cmd; di_node_t npa_node; di_minor_t npa_minor; diff --git a/usr/src/man/man8/nvmeadm.8 b/usr/src/man/man8/nvmeadm.8 index 4b40a01537..009b89bcf4 100644 --- a/usr/src/man/man8/nvmeadm.8 +++ b/usr/src/man/man8/nvmeadm.8 @@ -12,7 +12,7 @@ .\" Copyright 2021 Oxide Computer Company .\" Copyright 2022 Tintri by DDN, Inc. All rights reserved. .\" -.Dd February 15, 2022 +.Dd April 25, 2022 .Dt NVMEADM 8 .Os .Sh NAME @@ -52,8 +52,8 @@ .Nm .Op Fl dv .Cm secure-erase -.Ar ctl[/ns] .Op Fl c +.Ar ctl[/ns] .Nm .Op Fl dv .Cm detach @@ -359,8 +359,8 @@ command. .It Xo .Nm .Cm secure-erase -.Ar ctl[/ns] .Op Fl c +.Ar ctl[/ns] .Xc Erases the specified namespace or all namespaces of the controller. The flag -- cgit v1.2.3