Allow namespace-id specification where it makes sense.

It makes tool more convenient to not require user to explicitly convert
namespace device name into controller device name.  There should be no
changes to already existing syntaxes.

MFC after:	1 week
This commit is contained in:
mav 2020-04-20 14:54:41 +00:00
parent e367fe2912
commit 97e1aaaf76
4 changed files with 142 additions and 42 deletions

View File

@ -80,7 +80,7 @@ static const struct opts firmware_opts[] = {
#undef OPT
static const struct args firmware_args[] = {
{ arg_string, &opt.dev, "controller-id" },
{ arg_string, &opt.dev, "controller-id|namespace-id" },
{ arg_none, NULL, NULL },
};
@ -226,6 +226,7 @@ firmware(const struct cmd *f, int argc, char *argv[])
int activate_action, reboot_required;
char prompt[64];
void *buf = NULL;
char *path;
int32_t size = 0, nsid;
uint16_t oacs_fw;
uint8_t fw_slot1_ro, fw_num_slots;
@ -262,13 +263,12 @@ firmware(const struct cmd *f, int argc, char *argv[])
}
open_dev(opt.dev, &fd, 1, 1);
/* Check that a controller (and not a namespace) was specified. */
get_nsid(fd, NULL, &nsid);
get_nsid(fd, &path, &nsid);
if (nsid != 0) {
close(fd);
arg_help(argc, argv, f);
open_dev(path, &fd, 1, 1);
}
free(path);
read_controller_data(fd, &cdata);

View File

@ -79,7 +79,7 @@ static struct active_options {
};
static const struct args active_args[] = {
{ arg_string, &active_opt.dev, "controller-id" },
{ arg_string, &active_opt.dev, "controller-id|namespace-id" },
{ arg_none, NULL, NULL },
};
@ -112,7 +112,7 @@ static struct controllers_options {
};
static const struct args controllers_args[] = {
{ arg_string, &controllers_opt.dev, "controller-id" },
{ arg_string, &controllers_opt.dev, "controller-id|namespace-id" },
{ arg_none, NULL, NULL },
};
@ -178,7 +178,7 @@ static const struct opts create_opts[] = {
};
static const struct args create_args[] = {
{ arg_string, &create_opt.dev, "controller-id" },
{ arg_string, &create_opt.dev, "controller-id|namespace-id" },
{ arg_none, NULL, NULL },
};
@ -208,7 +208,7 @@ static const struct opts delete_opts[] = {
};
static const struct args delete_args[] = {
{ arg_string, &delete_opt.dev, "controller-id" },
{ arg_string, &delete_opt.dev, "controller-id|namespace-id" },
{ arg_none, NULL, NULL },
};
@ -242,7 +242,7 @@ static const struct opts attach_opts[] = {
};
static const struct args attach_args[] = {
{ arg_string, &attach_opt.dev, "controller-id" },
{ arg_string, &attach_opt.dev, "controller-id|namespace-id" },
{ arg_none, NULL, NULL },
};
@ -272,7 +272,7 @@ static const struct opts attached_opts[] = {
};
static const struct args attached_args[] = {
{ arg_string, &attached_opt.dev, "controller-id" },
{ arg_string, &attached_opt.dev, "controller-id|namespace-id" },
{ arg_none, NULL, NULL },
};
@ -306,7 +306,7 @@ static const struct opts detach_opts[] = {
};
static const struct args detach_args[] = {
{ arg_string, &detach_opt.dev, "controller-id" },
{ arg_string, &detach_opt.dev, "controller-id|namespace-id" },
{ arg_none, NULL, NULL },
};
@ -344,7 +344,7 @@ static const struct opts identify_opts[] = {
};
static const struct args identify_args[] = {
{ arg_string, &identify_opt.dev, "controller-id" },
{ arg_string, &identify_opt.dev, "controller-id|namespace-id" },
{ arg_none, NULL, NULL },
};
@ -399,12 +399,27 @@ static void
nsactive(const struct cmd *f, int argc, char *argv[])
{
struct nvme_pt_command pt;
struct nvme_controller_data cd;
int fd, i;
char *path;
uint32_t nsid;
uint32_t list[1024];
if (arg_parse(argc, argv, f))
return;
open_dev(active_opt.dev, &fd, 0, 1);
get_nsid(fd, &path, &nsid);
if (nsid != 0) {
close(fd);
open_dev(path, &fd, 0, 1);
}
free(path);
read_controller_data(fd, &cd);
/* Check that controller can execute this command. */
if (((cd.oacs >> NVME_CTRLR_DATA_OACS_NSMGMT_SHIFT) &
NVME_CTRLR_DATA_OACS_NSMGMT_MASK) == 0)
errx(1, "controller does not support namespace management");
memset(&pt, 0, sizeof(pt));
pt.cmd.opc = NVME_OPC_IDENTIFY;
@ -431,11 +446,19 @@ nsallocated(const struct cmd *f, int argc, char *argv[])
struct nvme_pt_command pt;
struct nvme_controller_data cd;
int fd, i;
char *path;
uint32_t nsid;
uint32_t list[1024];
if (arg_parse(argc, argv, f))
return;
open_dev(active_opt.dev, &fd, 0, 1);
get_nsid(fd, &path, &nsid);
if (nsid != 0) {
close(fd);
open_dev(path, &fd, 0, 1);
}
free(path);
read_controller_data(fd, &cd);
/* Check that controller can execute this command. */
@ -468,11 +491,19 @@ nscontrollers(const struct cmd *f, int argc, char *argv[])
struct nvme_pt_command pt;
struct nvme_controller_data cd;
int fd, i, n;
char *path;
uint32_t nsid;
uint16_t clist[2048];
if (arg_parse(argc, argv, f))
return;
open_dev(controllers_opt.dev, &fd, 0, 1);
get_nsid(fd, &path, &nsid);
if (nsid != 0) {
close(fd);
open_dev(path, &fd, 0, 1);
}
free(path);
read_controller_data(fd, &cd);
/* Check that controller can execute this command. */
@ -513,6 +544,8 @@ nscreate(const struct cmd *f, int argc, char *argv[])
struct nvme_controller_data cd;
struct nvme_namespace_data nsdata;
int fd, result;
char *path;
uint32_t nsid;
if (arg_parse(argc, argv, f))
return;
@ -526,6 +559,12 @@ nscreate(const struct cmd *f, int argc, char *argv[])
}
open_dev(create_opt.dev, &fd, 1, 1);
get_nsid(fd, &path, &nsid);
if (nsid != 0) {
close(fd);
open_dev(path, &fd, 1, 1);
}
free(path);
read_controller_data(fd, &cd);
/* Check that controller can execute this command. */
@ -583,17 +622,26 @@ nsdelete(const struct cmd *f, int argc, char *argv[])
struct nvme_pt_command pt;
struct nvme_controller_data cd;
int fd, result;
char *path;
uint32_t nsid;
char buf[2];
if (arg_parse(argc, argv, f))
return;
if (delete_opt.nsid == NONE) {
fprintf(stderr,
"No NSID specified");
arg_help(argc, argv, f);
}
open_dev(delete_opt.dev, &fd, 1, 1);
get_nsid(fd, &path, &nsid);
if (nsid != 0) {
close(fd);
open_dev(path, &fd, 1, 1);
} else if (delete_opt.nsid == NONE) {
close(fd);
fprintf(stderr, "No NSID specified");
arg_help(argc, argv, f);
}
if (delete_opt.nsid != NONE)
nsid = delete_opt.nsid;
free(path);
read_controller_data(fd, &cd);
/* Check that controller can execute this command. */
@ -607,7 +655,7 @@ nsdelete(const struct cmd *f, int argc, char *argv[])
pt.buf = buf;
pt.len = sizeof(buf);
pt.is_read = 1;
pt.cmd.nsid = delete_opt.nsid;
pt.cmd.nsid = nsid;
if ((result = ioctl(fd, NVME_PASSTHROUGH_CMD, &pt)) < 0)
errx(1, "ioctl request to %s failed: %d", delete_opt.dev, result);
@ -617,7 +665,7 @@ nsdelete(const struct cmd *f, int argc, char *argv[])
get_res_str((pt.cpl.status >> NVME_STATUS_SC_SHIFT) &
NVME_STATUS_SC_MASK));
}
printf("namespace %d deleted\n", delete_opt.nsid);
printf("namespace %d deleted\n", nsid);
exit(0);
}
@ -642,15 +690,24 @@ nsattach(const struct cmd *f, int argc, char *argv[])
struct nvme_pt_command pt;
struct nvme_controller_data cd;
int fd, result;
char *path;
uint32_t nsid;
uint16_t clist[2048];
if (arg_parse(argc, argv, f))
return;
if (attach_opt.nsid == NONE) {
fprintf(stderr, "No valid NSID specified\n");
open_dev(attach_opt.dev, &fd, 1, 1);
get_nsid(fd, &path, &nsid);
if (nsid != 0) {
close(fd);
open_dev(path, &fd, 1, 1);
} else if (attach_opt.nsid == NONE) {
close(fd);
fprintf(stderr, "No NSID specified");
arg_help(argc, argv, f);
}
open_dev(attach_opt.dev, &fd, 1, 1);
if (attach_opt.nsid != NONE)
nsid = attach_opt.nsid;
read_controller_data(fd, &cd);
/* Check that controller can execute this command. */
@ -682,7 +739,7 @@ nsattach(const struct cmd *f, int argc, char *argv[])
memset(&pt, 0, sizeof(pt));
pt.cmd.opc = NVME_OPC_NAMESPACE_ATTACHMENT;
pt.cmd.cdw10 = htole32(0); /* attach */
pt.cmd.nsid = attach_opt.nsid;
pt.cmd.nsid = nsid;
pt.buf = &clist;
pt.len = sizeof(clist);
@ -694,7 +751,7 @@ nsattach(const struct cmd *f, int argc, char *argv[])
get_res_str((pt.cpl.status >> NVME_STATUS_SC_SHIFT) &
NVME_STATUS_SC_MASK));
}
printf("namespace %d attached\n", attach_opt.nsid);
printf("namespace %d attached\n", nsid);
exit(0);
}
@ -704,15 +761,24 @@ nsdetach(const struct cmd *f, int argc, char *argv[])
struct nvme_pt_command pt;
struct nvme_controller_data cd;
int fd, result;
char *path;
uint32_t nsid;
uint16_t clist[2048];
if (arg_parse(argc, argv, f))
return;
if (detach_opt.nsid == NONE) {
fprintf(stderr, "No valid NSID specified\n");
open_dev(detach_opt.dev, &fd, 1, 1);
get_nsid(fd, &path, &nsid);
if (nsid != 0) {
close(fd);
open_dev(path, &fd, 1, 1);
} else if (detach_opt.nsid == NONE) {
close(fd);
fprintf(stderr, "No NSID specified");
arg_help(argc, argv, f);
}
open_dev(detach_opt.dev, &fd, 1, 1);
if (detach_opt.nsid != NONE)
nsid = detach_opt.nsid;
read_controller_data(fd, &cd);
/* Check that controller can execute this command. */
@ -724,7 +790,7 @@ nsdetach(const struct cmd *f, int argc, char *argv[])
/* Get list of controllers this namespace attached to. */
memset(&pt, 0, sizeof(pt));
pt.cmd.opc = NVME_OPC_IDENTIFY;
pt.cmd.nsid = htole32(detach_opt.nsid);
pt.cmd.nsid = htole32(nsid);
pt.cmd.cdw10 = htole32(0x12);
pt.buf = clist;
pt.len = sizeof(clist);
@ -751,7 +817,7 @@ nsdetach(const struct cmd *f, int argc, char *argv[])
memset(&pt, 0, sizeof(pt));
pt.cmd.opc = NVME_OPC_NAMESPACE_ATTACHMENT;
pt.cmd.cdw10 = htole32(1); /* detach */
pt.cmd.nsid = detach_opt.nsid;
pt.cmd.nsid = nsid;
pt.buf = &clist;
pt.len = sizeof(clist);
@ -763,7 +829,7 @@ nsdetach(const struct cmd *f, int argc, char *argv[])
get_res_str((pt.cpl.status >> NVME_STATUS_SC_SHIFT) &
NVME_STATUS_SC_MASK));
}
printf("namespace %d detached\n", detach_opt.nsid);
printf("namespace %d detached\n", nsid);
exit(0);
}
@ -773,15 +839,24 @@ nsattached(const struct cmd *f, int argc, char *argv[])
struct nvme_pt_command pt;
struct nvme_controller_data cd;
int fd, i, n;
char *path;
uint32_t nsid;
uint16_t clist[2048];
if (arg_parse(argc, argv, f))
return;
if (attached_opt.nsid == NONE) {
fprintf(stderr, "No valid NSID specified\n");
open_dev(attached_opt.dev, &fd, 0, 1);
get_nsid(fd, &path, &nsid);
if (nsid != 0) {
close(fd);
open_dev(path, &fd, 1, 1);
} else if (attached_opt.nsid == NONE) {
close(fd);
fprintf(stderr, "No NSID specified");
arg_help(argc, argv, f);
}
open_dev(attached_opt.dev, &fd, 0, 1);
if (attached_opt.nsid != NONE)
nsid = attached_opt.nsid;
read_controller_data(fd, &cd);
/* Check that controller can execute this command. */
@ -791,7 +866,7 @@ nsattached(const struct cmd *f, int argc, char *argv[])
memset(&pt, 0, sizeof(pt));
pt.cmd.opc = NVME_OPC_IDENTIFY;
pt.cmd.nsid = htole32(attached_opt.nsid);
pt.cmd.nsid = htole32(nsid);
pt.cmd.cdw10 = htole32(0x12);
pt.buf = clist;
pt.len = sizeof(clist);
@ -817,15 +892,24 @@ nsidentify(const struct cmd *f, int argc, char *argv[])
struct nvme_namespace_data nsdata;
uint8_t *data;
int fd;
char *path;
uint32_t nsid;
u_int i;
if (arg_parse(argc, argv, f))
return;
if (identify_opt.nsid == NONE) {
fprintf(stderr, "No valid NSID specified\n");
open_dev(identify_opt.dev, &fd, 0, 1);
get_nsid(fd, &path, &nsid);
if (nsid != 0) {
close(fd);
open_dev(path, &fd, 1, 1);
} else if (identify_opt.nsid == NONE) {
close(fd);
fprintf(stderr, "No NSID specified");
arg_help(argc, argv, f);
}
open_dev(identify_opt.dev, &fd, 0, 1);
if (identify_opt.nsid != NONE)
nsid = identify_opt.nsid;
read_controller_data(fd, &cd);
/* Check that controller can execute this command. */
@ -835,7 +919,7 @@ nsidentify(const struct cmd *f, int argc, char *argv[])
memset(&pt, 0, sizeof(pt));
pt.cmd.opc = NVME_OPC_IDENTIFY;
pt.cmd.nsid = htole32(identify_opt.nsid);
pt.cmd.nsid = htole32(nsid);
pt.cmd.cdw10 = htole32(0x11);
pt.buf = &nsdata;
pt.len = sizeof(nsdata);
@ -855,7 +939,7 @@ nsidentify(const struct cmd *f, int argc, char *argv[])
break;
}
if (i == sizeof(nsdata))
errx(1, "namespace %d is not allocated", identify_opt.nsid);
errx(1, "namespace %d is not allocated", nsid);
/* Convert data to host endian */
nvme_namespace_data_swapbytes(&nsdata);

View File

@ -144,6 +144,8 @@ power(const struct cmd *f, int argc, char *argv[])
{
struct nvme_controller_data cdata;
int fd;
char *path;
uint32_t nsid;
if (arg_parse(argc, argv, f))
return;
@ -154,6 +156,12 @@ power(const struct cmd *f, int argc, char *argv[])
}
open_dev(opt.dev, &fd, 1, 1);
get_nsid(fd, &path, &nsid);
if (nsid != 0) {
close(fd);
open_dev(path, &fd, 1, 1);
}
free(path);
if (opt.list) {
read_controller_data(fd, &cdata);
@ -185,7 +193,7 @@ static const struct opts power_opts[] = {
#undef OPT
static const struct args power_args[] = {
{ arg_string, &opt.dev, "controller-id" },
{ arg_string, &opt.dev, "controller-id|namespace-id" },
{ arg_none, NULL, NULL },
};

View File

@ -48,7 +48,7 @@ static struct options {
};
static const struct args args[] = {
{ arg_string, &opt.dev, "controller-id" },
{ arg_string, &opt.dev, "controller-id|namespace-id" },
{ arg_none, NULL, NULL },
};
@ -56,10 +56,18 @@ static void
reset(const struct cmd *f, int argc, char *argv[])
{
int fd;
char *path;
uint32_t nsid;
if (arg_parse(argc, argv, f))
return;
open_dev(opt.dev, &fd, 1, 1);
get_nsid(fd, &path, &nsid);
if (nsid != 0) {
close(fd);
open_dev(path, &fd, 1, 1);
}
free(path);
if (ioctl(fd, NVME_RESET_CONTROLLER) < 0)
err(1, "reset request to %s failed", argv[optind]);