Usage cleanup pt 1

Provide a usage() function that takes a struct nvme_function pointer
and produces a usage mssage. Eliminate all now-redundant usage
functions. Propigate the new argument through the program as needed.
Use common routine to print usage.

Sponsored by: Netflix
Differential Revision: https://reviews.freebsd.org/D18403
This commit is contained in:
Warner Losh 2018-12-02 23:12:58 +00:00
parent fbf14fe84b
commit 7d923c13d7
12 changed files with 96 additions and 183 deletions

View File

@ -46,14 +46,6 @@ __FBSDID("$FreeBSD$");
#define DEVLIST_USAGE \
" nvmecontrol devlist\n"
static void
devlist_usage(void)
{
fprintf(stderr, "usage:\n");
fprintf(stderr, DEVLIST_USAGE);
exit(1);
}
static inline uint32_t
ns_get_sector_size(struct nvme_namespace_data *nsdata)
{
@ -68,7 +60,7 @@ ns_get_sector_size(struct nvme_namespace_data *nsdata)
}
static void
devlist(int argc, char *argv[])
devlist(struct nvme_function *nf, int argc, char *argv[])
{
struct nvme_controller_data cdata;
struct nvme_namespace_data nsdata;
@ -80,7 +72,7 @@ devlist(int argc, char *argv[])
while ((ch = getopt(argc, argv, "")) != -1) {
switch ((char)ch) {
default:
devlist_usage();
usage(nf);
}
}

View File

@ -53,7 +53,6 @@ __FBSDID("$FreeBSD$");
#define FIRMWARE_USAGE \
" nvmecontrol firmware [-s slot] [-f path_to_firmware] [-a] <controller id>\n"
static int
slot_has_valid_firmware(int fd, int slot)
{
@ -175,15 +174,7 @@ activate_firmware(int fd, int slot, int activate_action)
}
static void
firmware_usage(void)
{
fprintf(stderr, "usage:\n");
fprintf(stderr, FIRMWARE_USAGE);
exit(1);
}
static void
firmware(int argc, char *argv[])
firmware(struct nvme_function *nf, int argc, char *argv[])
{
int fd = -1, slot = 0;
int a_flag, s_flag, f_flag;
@ -210,18 +201,18 @@ firmware(int argc, char *argv[])
fprintf(stderr,
"\"%s\" not valid slot.\n",
optarg);
firmware_usage();
usage(nf);
} else if (slot == 0) {
fprintf(stderr,
"0 is not a valid slot number. "
"Slot numbers start at 1.\n");
firmware_usage();
usage(nf);
} else if (slot > 7) {
fprintf(stderr,
"Slot number %s specified which is "
"greater than max allowed slot number of "
"7.\n", optarg);
firmware_usage();
usage(nf);
}
s_flag = true;
break;
@ -234,20 +225,20 @@ firmware(int argc, char *argv[])
/* Check that a controller (and not a namespace) was specified. */
if (optind >= argc || strstr(argv[optind], NVME_NS_PREFIX) != NULL)
firmware_usage();
usage(nf);
if (!f_flag && !a_flag) {
fprintf(stderr,
"Neither a replace ([-f path_to_firmware]) nor "
"activate ([-a]) firmware image action\n"
"was specified.\n");
firmware_usage();
usage(nf);
}
if (!f_flag && a_flag && slot == 0) {
fprintf(stderr,
"Slot number to activate not specified.\n");
firmware_usage();
usage(nf);
}
controller = argv[optind];

View File

@ -47,15 +47,7 @@ __FBSDID("$FreeBSD$");
" nvmecontrol format [-f fmt] [-m mset] [-p pi] [-l pil] [-E] [-C] <controller id|namespace id>\n"
static void
format_usage(void)
{
fprintf(stderr, "usage:\n");
fprintf(stderr, FORMAT_USAGE);
exit(1);
}
static void
format(int argc, char *argv[])
format(struct nvme_function *nf, int argc, char *argv[])
{
struct nvme_controller_data cd;
struct nvme_namespace_data nsd;
@ -67,7 +59,7 @@ format(int argc, char *argv[])
int lbaf = -1, mset = -1, pi = -1, pil = -1, ses = 0;
if (argc < 2)
format_usage();
usage(nf);
while ((ch = getopt(argc, argv, "f:m:p:l:EC")) != -1) {
switch ((char)ch) {
@ -94,13 +86,13 @@ format(int argc, char *argv[])
ses = 2;
break;
default:
format_usage();
usage(nf);
}
}
/* Check that a controller or namespace was specified. */
if (optind >= argc)
format_usage();
usage(nf);
target = argv[optind];
/*

View File

@ -150,15 +150,7 @@ print_namespace(struct nvme_namespace_data *nsdata)
}
static void
identify_usage(void)
{
fprintf(stderr, "usage:\n");
fprintf(stderr, IDENTIFY_USAGE);
exit(1);
}
static void
identify_ctrlr(int argc, char *argv[])
identify_ctrlr(struct nvme_function *nf, int argc, char *argv[])
{
struct nvme_controller_data cdata;
int ch, fd, hexflag = 0, hexlength;
@ -173,13 +165,13 @@ identify_ctrlr(int argc, char *argv[])
hexflag = 1;
break;
default:
identify_usage();
usage(nf);
}
}
/* Check that a controller was specified. */
if (optind >= argc)
identify_usage();
usage(nf);
open_dev(argv[optind], &fd, 1, 1);
read_controller_data(fd, &cdata);
@ -197,7 +189,7 @@ identify_ctrlr(int argc, char *argv[])
if (verboseflag == 1) {
fprintf(stderr, "-v not currently supported without -x\n");
identify_usage();
usage(nf);
}
nvme_print_controller(&cdata);
@ -205,7 +197,7 @@ identify_ctrlr(int argc, char *argv[])
}
static void
identify_ns(int argc, char *argv[])
identify_ns(struct nvme_function *nf,int argc, char *argv[])
{
struct nvme_namespace_data nsdata;
char path[64];
@ -222,13 +214,13 @@ identify_ns(int argc, char *argv[])
hexflag = 1;
break;
default:
identify_usage();
usage(nf);
}
}
/* Check that a namespace was specified. */
if (optind >= argc)
identify_usage();
usage(nf);
/*
* Check if the specified device node exists before continuing.
@ -261,7 +253,7 @@ identify_ns(int argc, char *argv[])
if (verboseflag == 1) {
fprintf(stderr, "-v not currently supported without -x\n");
identify_usage();
usage(nf);
}
print_namespace(&nsdata);
@ -269,18 +261,18 @@ identify_ns(int argc, char *argv[])
}
static void
identify(int argc, char *argv[])
identify(struct nvme_function *nf, int argc, char *argv[])
{
char *target;
if (argc < 2)
identify_usage();
usage(nf);
while (getopt(argc, argv, "vx") != -1) ;
/* Check that a controller or namespace was specified. */
if (optind >= argc)
identify_usage();
usage(nf);
target = argv[optind];
@ -292,9 +284,9 @@ identify(int argc, char *argv[])
* otherwise, consider it a controller.
*/
if (strstr(target, NVME_NS_PREFIX) == NULL)
identify_ctrlr(argc, argv);
identify_ctrlr(nf, argc, argv);
else
identify_ns(argc, argv);
identify_ns(nf, argc, argv);
}
NVME_COMMAND(top, identify, identify, IDENTIFY_USAGE);

View File

@ -472,14 +472,6 @@ NVME_LOGPAGE(samsung_smart,
INTEL_LOG_ADD_SMART, "samsung", "Extra Health/SMART Data",
print_intel_add_smart, DEFAULT_SIZE);
static void
logpage_usage(void)
{
fprintf(stderr, "usage:\n");
fprintf(stderr, LOGPAGE_USAGE);
exit(1);
}
static void
logpage_help(void)
{
@ -498,7 +490,7 @@ logpage_help(void)
}
static void
logpage(int argc, char *argv[])
logpage(struct nvme_function *nf, int argc, char *argv[])
{
int fd;
int log_page = 0, pageflag = false;
@ -529,7 +521,7 @@ logpage(int argc, char *argv[])
fprintf(stderr,
"\"%s\" not valid log page id.\n",
optarg);
logpage_usage();
usage(nf);
}
pageflag = true;
break;
@ -546,12 +538,12 @@ logpage(int argc, char *argv[])
if (!pageflag) {
printf("Missing page_id (-p).\n");
logpage_usage();
usage(nf);
}
/* Check that a controller and/or namespace was specified. */
if (optind >= argc)
logpage_usage();
usage(nf);
if (strstr(argv[optind], NVME_NS_PREFIX) != NULL) {
ns_specified = true;

View File

@ -60,48 +60,16 @@ SET_DECLARE(ns, struct nvme_function);
#define NSDETACH_USAGE \
" nvmecontrol ns detach -n nsid [-c ctrlrid] nvmeN\n"
void nscreate(int argc, char *argv[]);
void nsdelete(int argc, char *argv[]);
void nsattach(int argc, char *argv[]);
void nsdetach(int argc, char *argv[]);
void nscreate(struct nvme_function *nf, int argc, char *argv[]);
void nsdelete(struct nvme_function *nf, int argc, char *argv[]);
void nsattach(struct nvme_function *nf, int argc, char *argv[]);
void nsdetach(struct nvme_function *nf, int argc, char *argv[]);
NVME_COMMAND(ns, create, nscreate, NSCREATE_USAGE);
NVME_COMMAND(ns, delete, nsdelete, NSDELETE_USAGE);
NVME_COMMAND(ns, attach, nsattach, NSATTACH_USAGE);
NVME_COMMAND(ns, detach, nsdetach, NSDETACH_USAGE);
static void
nscreate_usage(void)
{
fprintf(stderr, "usage:\n");
fprintf(stderr, NSCREATE_USAGE);
exit(1);
}
static void
nsdelete_usage(void)
{
fprintf(stderr, "usage:\n");
fprintf(stderr, NSDELETE_USAGE);
exit(1);
}
static void
nsattach_usage(void)
{
fprintf(stderr, "usage:\n");
fprintf(stderr, NSATTACH_USAGE);
exit(1);
}
static void
nsdetach_usage(void)
{
fprintf(stderr, "usage:\n");
fprintf(stderr, NSDETACH_USAGE);
exit(1);
}
struct ns_result_str {
uint16_t res;
const char * str;
@ -142,7 +110,7 @@ get_res_str(uint16_t res)
* 0xb = Thin Provisioning Not supported
*/
void
nscreate(int argc, char *argv[])
nscreate(struct nvme_function *nf, int argc, char *argv[])
{
struct nvme_pt_command pt;
struct nvme_controller_data cd;
@ -151,7 +119,7 @@ nscreate(int argc, char *argv[])
int ch, fd, result, lbaf = 0, mset = 0, nmic = -1, pi = 0, pil = 0;
if (optind >= argc)
nscreate_usage();
usage(nf);
while ((ch = getopt(argc, argv, "s:c:f:m:n:p:l:")) != -1) {
switch (ch) {
@ -177,17 +145,17 @@ nscreate(int argc, char *argv[])
pil = strtol(optarg, NULL, 0);
break;
default:
nscreate_usage();
usage(nf);
}
}
if (optind >= argc)
nscreate_usage();
usage(nf);
if (cap == -1)
cap = nsze;
if (nsze == -1 || cap == -1)
nscreate_usage();
usage(nf);
open_dev(argv[optind], &fd, 1, 1);
read_controller_data(fd, &cd);
@ -237,7 +205,7 @@ nscreate(int argc, char *argv[])
}
void
nsdelete(int argc, char *argv[])
nsdelete(struct nvme_function *nf, int argc, char *argv[])
{
struct nvme_pt_command pt;
struct nvme_controller_data cd;
@ -245,7 +213,7 @@ nsdelete(int argc, char *argv[])
char buf[2];
if (optind >= argc)
nsdelete_usage();
usage(nf);
while ((ch = getopt(argc, argv, "n:")) != -1) {
switch ((char)ch) {
@ -253,12 +221,12 @@ nsdelete(int argc, char *argv[])
nsid = strtol(optarg, (char **)NULL, 0);
break;
default:
nsdelete_usage();
usage(nf);
}
}
if (optind >= argc || nsid == -2)
nsdelete_usage();
usage(nf);
open_dev(argv[optind], &fd, 1, 1);
read_controller_data(fd, &cd);
@ -304,7 +272,7 @@ nsdelete(int argc, char *argv[])
* 0x2 Invalid Field can occur if ctrlrid d.n.e in system.
*/
void
nsattach(int argc, char *argv[])
nsattach(struct nvme_function *nf, int argc, char *argv[])
{
struct nvme_pt_command pt;
struct nvme_controller_data cd;
@ -313,7 +281,7 @@ nsattach(int argc, char *argv[])
uint16_t clist[2048];
if (optind >= argc)
nsattach_usage();
usage(nf);
while ((ch = getopt(argc, argv, "n:c:")) != -1) {
switch (ch) {
@ -324,15 +292,15 @@ nsattach(int argc, char *argv[])
ctrlrid = strtol(optarg, (char **)NULL, 0);
break;
default:
nsattach_usage();
usage(nf);
}
}
if (optind >= argc)
nsattach_usage();
usage(nf);
if (nsid == -1 )
nsattach_usage();
usage(nf);
open_dev(argv[optind], &fd, 1, 1);
read_controller_data(fd, &cd);
@ -383,7 +351,7 @@ nsattach(int argc, char *argv[])
}
void
nsdetach(int argc, char *argv[])
nsdetach(struct nvme_function *nf, int argc, char *argv[])
{
struct nvme_pt_command pt;
struct nvme_controller_data cd;
@ -392,7 +360,7 @@ nsdetach(int argc, char *argv[])
uint16_t clist[2048];
if (optind >= argc)
nsdetach_usage();
usage(nf);
while ((ch = getopt(argc, argv, "n:c:")) != -1) {
switch (ch) {
@ -403,15 +371,15 @@ nsdetach(int argc, char *argv[])
ctrlrid = strtol(optarg, (char **)NULL, 0);
break;
default:
nsdetach_usage();
usage(nf);
}
}
if (optind >= argc)
nsdetach_usage();
usage(nf);
if (nsid == -1)
nsdetach_usage();
usage(nf);
open_dev(argv[optind], &fd, 1, 1);
read_controller_data(fd, &cd);
@ -469,7 +437,7 @@ nsdetach(int argc, char *argv[])
}
static void
ns(int argc, char *argv[])
ns(struct nvme_function *nf __unused, int argc, char *argv[])
{
DISPATCH(argc, argv, ns);

View File

@ -49,18 +49,33 @@ __FBSDID("$FreeBSD$");
SET_DECLARE(top, struct nvme_function);
static void
print_usage(const struct nvme_function *f)
{
fprintf(stderr, "%s", f->usage);
}
static void
gen_usage_set(struct nvme_function **f, struct nvme_function **flimit)
{
fprintf(stderr, "usage:\n");
while (f < flimit) {
fprintf(stderr, "%s", (*f)->usage);
print_usage(*f);
f++;
}
exit(1);
}
void
usage(const struct nvme_function *f)
{
fprintf(stderr, "usage:\n");
print_usage(f);
exit(1);
}
void
dispatch_set(int argc, char *argv[], struct nvme_function **tbl,
struct nvme_function **tbl_limit)
@ -74,7 +89,7 @@ dispatch_set(int argc, char *argv[], struct nvme_function **tbl,
while (f < tbl_limit) {
if (strcmp(argv[1], (*f)->name) == 0) {
(*f)->fn(argc-1, &argv[1]);
(*f)->fn(*f, argc-1, &argv[1]);
return;
}
f++;

View File

@ -34,7 +34,8 @@
#include <sys/linker_set.h>
#include <dev/nvme/nvme.h>
typedef void (*nvme_fn_t)(int argc, char *argv[]);
struct nvme_function;
typedef void (*nvme_fn_t)(struct nvme_function *nf, int argc, char *argv[]);
struct nvme_function {
const char *name;
@ -88,6 +89,7 @@ void print_hex(void *data, uint32_t length);
void read_logpage(int fd, uint8_t log_page, uint32_t nsid, void *payload,
uint32_t payload_size);
void usage(const struct nvme_function *f);
void dispatch_set(int argc, char *argv[], struct nvme_function **tbl,
struct nvme_function **tbl_limit);

View File

@ -75,15 +75,7 @@ print_perftest(struct nvme_io_test *io_test, bool perthread)
}
static void
perftest_usage(void)
{
fprintf(stderr, "usage:\n");
fprintf(stderr, PERFTEST_USAGE);
exit(1);
}
static void
perftest(int argc, char *argv[])
perftest(struct nvme_function *nf, int argc, char *argv[])
{
struct nvme_io_test io_test;
int fd;
@ -118,13 +110,13 @@ perftest(int argc, char *argv[])
fprintf(stderr,
"\"%s\" not valid number of threads.\n",
optarg);
perftest_usage();
usage(nf);
} else if (io_test.num_threads == 0 ||
io_test.num_threads > 128) {
fprintf(stderr,
"\"%s\" not valid number of threads.\n",
optarg);
perftest_usage();
usage(nf);
}
break;
case 'o':
@ -137,7 +129,7 @@ perftest(int argc, char *argv[])
else {
fprintf(stderr, "\"%s\" not valid opcode.\n",
optarg);
perftest_usage();
usage(nf);
}
break;
case 'p':
@ -155,7 +147,7 @@ perftest(int argc, char *argv[])
} else {
fprintf(stderr, "\"%s\" not valid size.\n",
optarg);
perftest_usage();
usage(nf);
}
break;
case 't':
@ -165,14 +157,15 @@ perftest(int argc, char *argv[])
fprintf(stderr,
"\"%s\" not valid time duration.\n",
optarg);
perftest_usage();
usage(nf);
}
break;
}
}
if (!nflag || !oflag || !sflag || !tflag || optind >= argc)
perftest_usage();
usage(nf);
open_dev(argv[optind], &fd, 1, 1);
if (ioctl(fd, ioctl_cmd, &io_test) < 0)

View File

@ -47,14 +47,6 @@ _Static_assert(sizeof(struct nvme_power_state) == 256 / NBBY,
#define POWER_USAGE \
" nvmecontrol power [-l] [-p new-state [-w workload-hint]] <controller id>\n"
static void
power_usage(void)
{
fprintf(stderr, "usage:\n");
fprintf(stderr, POWER_USAGE);
exit(1);
}
static void
power_list_one(int i, struct nvme_power_state *nps)
{
@ -137,7 +129,7 @@ power_show(int fd)
}
static void
power(int argc, char *argv[])
power(struct nvme_function *nf, int argc, char *argv[])
{
struct nvme_controller_data cdata;
int ch, listflag = 0, powerflag = 0, power_val = 0, fd;
@ -154,28 +146,28 @@ power(int argc, char *argv[])
power_val = strtol(optarg, &end, 0);
if (*end != '\0') {
fprintf(stderr, "Invalid power state number: %s\n", optarg);
power_usage();
usage(nf);
}
break;
case 'w':
workload = strtol(optarg, &end, 0);
if (*end != '\0') {
fprintf(stderr, "Invalid workload hint: %s\n", optarg);
power_usage();
usage(nf);
}
break;
default:
power_usage();
usage(nf);
}
}
/* Check that a controller was specified. */
if (optind >= argc)
power_usage();
usage(nf);
if (listflag && powerflag) {
fprintf(stderr, "Can't set power and list power states\n");
power_usage();
usage(nf);
}
open_dev(argv[optind], &fd, 1, 1);

View File

@ -45,28 +45,20 @@ __FBSDID("$FreeBSD$");
" nvmecontrol reset <controller id>\n"
static void
reset_usage(void)
{
fprintf(stderr, "usage:\n");
fprintf(stderr, RESET_USAGE);
exit(1);
}
static void
reset(int argc, char *argv[])
reset(struct nvme_function *nf, int argc, char *argv[])
{
int ch, fd;
while ((ch = getopt(argc, argv, "")) != -1) {
switch ((char)ch) {
default:
reset_usage();
usage(nf);
}
}
/* Check that a controller was specified. */
if (optind >= argc)
reset_usage();
usage(nf);
open_dev(argv[optind], &fd, 1, 1);
if (ioctl(fd, NVME_RESET_CONTROLLER) < 0)

View File

@ -52,7 +52,7 @@ SET_DECLARE(wdc, struct nvme_function);
#define WDC_NVME_CAP_DIAG_OPCODE 0xe6
#define WDC_NVME_CAP_DIAG_CMD 0x0000
static void wdc_cap_diag(int argc, char *argv[]);
static void wdc_cap_diag(struct nvme_function *nf, int argc, char *argv[]);
#define WDC_CAP_DIAG_USAGE "\tnvmecontrol wdc cap-diag [-o path-template]\n"
@ -154,15 +154,7 @@ wdc_do_dump(int fd, char *tmpl, const char *suffix, uint32_t opcode,
}
static void
wdc_cap_diag_usage(void)
{
fprintf(stderr, "usage:\n");
fprintf(stderr, WDC_CAP_DIAG_USAGE);
exit(1);
}
static void
wdc_cap_diag(int argc, char *argv[])
wdc_cap_diag(struct nvme_function *nf, int argc, char *argv[])
{
char path_tmpl[MAXPATHLEN];
int ch, fd;
@ -174,12 +166,12 @@ wdc_cap_diag(int argc, char *argv[])
strlcpy(path_tmpl, optarg, MAXPATHLEN);
break;
default:
wdc_cap_diag_usage();
usage(nf);
}
}
/* Check that a controller was specified. */
if (optind >= argc)
wdc_cap_diag_usage();
usage(nf);
open_dev(argv[optind], &fd, 1, 1);
wdc_do_dump(fd, path_tmpl, "cap_diag", WDC_NVME_CAP_DIAG_OPCODE,
@ -191,7 +183,7 @@ wdc_cap_diag(int argc, char *argv[])
}
static void
wdc(int argc, char *argv[])
wdc(struct nvme_function *nf __unused, int argc, char *argv[])
{
DISPATCH(argc, argv, wdc);