Open device with O_RDONLY when command is non-invasive.

This allows to use some of the subcommands against mounted nvd devices.

MFC after:	1 week
Sponsored by:	iXystems, Inc.
This commit is contained in:
Alexander Motin 2020-04-20 13:47:07 +00:00
parent 21eefd310a
commit 1f15d49eea
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=360117
7 changed files with 21 additions and 22 deletions

View File

@ -241,7 +241,7 @@ identify(const struct cmd *f, int argc, char *argv[])
if (arg_parse(argc, argv, f)) if (arg_parse(argc, argv, f))
return; return;
open_dev(opt.dev, &fd, 1, 1); open_dev(opt.dev, &fd, 0, 1);
get_nsid(fd, &path, &nsid); get_nsid(fd, &path, &nsid);
if (nsid != 0) { if (nsid != 0) {
/* /*
@ -251,7 +251,7 @@ identify(const struct cmd *f, int argc, char *argv[])
* the IDENTIFY command itself. * the IDENTIFY command itself.
*/ */
close(fd); close(fd);
open_dev(path, &fd, 1, 1); open_dev(path, &fd, 0, 1);
} }
free(path); free(path);
if (opt.nsid != NONE) if (opt.nsid != NONE)

View File

@ -687,13 +687,13 @@ logpage(const struct cmd *f, int argc, char *argv[])
fprintf(stderr, "Missing page_id (-p).\n"); fprintf(stderr, "Missing page_id (-p).\n");
arg_help(argc, argv, f); arg_help(argc, argv, f);
} }
open_dev(opt.dev, &fd, 1, 1); open_dev(opt.dev, &fd, 0, 1);
get_nsid(fd, &path, &nsid); get_nsid(fd, &path, &nsid);
if (nsid == 0) { if (nsid == 0) {
nsid = NVME_GLOBAL_NAMESPACE_TAG; nsid = NVME_GLOBAL_NAMESPACE_TAG;
} else { } else {
close(fd); close(fd);
open_dev(path, &fd, 1, 1); open_dev(path, &fd, 0, 1);
} }
free(path); free(path);

View File

@ -404,7 +404,7 @@ nsactive(const struct cmd *f, int argc, char *argv[])
if (arg_parse(argc, argv, f)) if (arg_parse(argc, argv, f))
return; return;
open_dev(active_opt.dev, &fd, 1, 1); open_dev(active_opt.dev, &fd, 0, 1);
memset(&pt, 0, sizeof(pt)); memset(&pt, 0, sizeof(pt));
pt.cmd.opc = NVME_OPC_IDENTIFY; pt.cmd.opc = NVME_OPC_IDENTIFY;
@ -435,7 +435,7 @@ nsallocated(const struct cmd *f, int argc, char *argv[])
if (arg_parse(argc, argv, f)) if (arg_parse(argc, argv, f))
return; return;
open_dev(active_opt.dev, &fd, 1, 1); open_dev(active_opt.dev, &fd, 0, 1);
read_controller_data(fd, &cd); read_controller_data(fd, &cd);
/* Check that controller can execute this command. */ /* Check that controller can execute this command. */
@ -472,7 +472,7 @@ nscontrollers(const struct cmd *f, int argc, char *argv[])
if (arg_parse(argc, argv, f)) if (arg_parse(argc, argv, f))
return; return;
open_dev(controllers_opt.dev, &fd, 1, 1); open_dev(controllers_opt.dev, &fd, 0, 1);
read_controller_data(fd, &cd); read_controller_data(fd, &cd);
/* Check that controller can execute this command. */ /* Check that controller can execute this command. */
@ -781,7 +781,7 @@ nsattached(const struct cmd *f, int argc, char *argv[])
fprintf(stderr, "No valid NSID specified\n"); fprintf(stderr, "No valid NSID specified\n");
arg_help(argc, argv, f); arg_help(argc, argv, f);
} }
open_dev(attached_opt.dev, &fd, 1, 1); open_dev(attached_opt.dev, &fd, 0, 1);
read_controller_data(fd, &cd); read_controller_data(fd, &cd);
/* Check that controller can execute this command. */ /* Check that controller can execute this command. */
@ -825,7 +825,7 @@ nsidentify(const struct cmd *f, int argc, char *argv[])
fprintf(stderr, "No valid NSID specified\n"); fprintf(stderr, "No valid NSID specified\n");
arg_help(argc, argv, f); arg_help(argc, argv, f);
} }
open_dev(identify_opt.dev, &fd, 1, 1); open_dev(identify_opt.dev, &fd, 0, 1);
read_controller_data(fd, &cd); read_controller_data(fd, &cd);
/* Check that controller can execute this command. */ /* Check that controller can execute this command. */

View File

@ -73,7 +73,7 @@ gnsid(const struct cmd *f, int argc, char *argv[])
if (arg_parse(argc, argv, f)) if (arg_parse(argc, argv, f))
return; return;
open_dev(nsid_opt.dev, &fd, 1, 1); open_dev(nsid_opt.dev, &fd, 0, 1);
get_nsid(fd, &path, &nsid); get_nsid(fd, &path, &nsid);
close(fd); close(fd);
printf("%s\t%u\n", path, nsid); printf("%s\t%u\n", path, nsid);

View File

@ -142,18 +142,17 @@ read_namespace_data(int fd, uint32_t nsid, struct nvme_namespace_data *nsdata)
} }
int int
open_dev(const char *str, int *fd, int show_error, int exit_on_error) open_dev(const char *str, int *fd, int write, int exit_on_error)
{ {
char full_path[64]; char full_path[64];
snprintf(full_path, sizeof(full_path), _PATH_DEV"%s", str); snprintf(full_path, sizeof(full_path), _PATH_DEV"%s", str);
*fd = open(full_path, O_RDWR); *fd = open(full_path, write ? O_RDWR : O_RDONLY);
if (*fd < 0) { if (*fd < 0) {
if (show_error) if (exit_on_error) {
warn("could not open %s", full_path); err(1, "could not open %s%s", full_path,
if (exit_on_error) write ? " for write" : "");
exit(1); } else
else
return (errno); return (errno);
} }

View File

@ -68,7 +68,7 @@ void logpage_register(struct logpage_function *p);
#define NVME_CTRLR_PREFIX "nvme" #define NVME_CTRLR_PREFIX "nvme"
#define NVME_NS_PREFIX "ns" #define NVME_NS_PREFIX "ns"
int open_dev(const char *str, int *fd, int show_error, int exit_on_error); int open_dev(const char *str, int *fd, int write, int exit_on_error);
void get_nsid(int fd, char **ctrlr_str, uint32_t *nsid); void get_nsid(int fd, char **ctrlr_str, uint32_t *nsid);
void read_controller_data(int fd, struct nvme_controller_data *cdata); void read_controller_data(int fd, struct nvme_controller_data *cdata);
void read_namespace_data(int fd, uint32_t nsid, struct nvme_namespace_data *nsdata); void read_namespace_data(int fd, uint32_t nsid, struct nvme_namespace_data *nsdata);

View File

@ -242,7 +242,7 @@ resvacquire(const struct cmd *f, int argc, char *argv[])
if (arg_parse(argc, argv, f)) if (arg_parse(argc, argv, f))
return; return;
open_dev(acquire_opt.dev, &fd, 1, 1); open_dev(acquire_opt.dev, &fd, 0, 1);
get_nsid(fd, NULL, &nsid); get_nsid(fd, NULL, &nsid);
if (nsid == 0) { if (nsid == 0) {
fprintf(stderr, "This command require namespace-id\n"); fprintf(stderr, "This command require namespace-id\n");
@ -280,7 +280,7 @@ resvregister(const struct cmd *f, int argc, char *argv[])
if (arg_parse(argc, argv, f)) if (arg_parse(argc, argv, f))
return; return;
open_dev(register_opt.dev, &fd, 1, 1); open_dev(register_opt.dev, &fd, 0, 1);
get_nsid(fd, NULL, &nsid); get_nsid(fd, NULL, &nsid);
if (nsid == 0) { if (nsid == 0) {
fprintf(stderr, "This command require namespace-id\n"); fprintf(stderr, "This command require namespace-id\n");
@ -318,7 +318,7 @@ resvrelease(const struct cmd *f, int argc, char *argv[])
if (arg_parse(argc, argv, f)) if (arg_parse(argc, argv, f))
return; return;
open_dev(release_opt.dev, &fd, 1, 1); open_dev(release_opt.dev, &fd, 0, 1);
get_nsid(fd, NULL, &nsid); get_nsid(fd, NULL, &nsid);
if (nsid == 0) { if (nsid == 0) {
fprintf(stderr, "This command require namespace-id\n"); fprintf(stderr, "This command require namespace-id\n");
@ -358,7 +358,7 @@ resvreport(const struct cmd *f, int argc, char *argv[])
if (arg_parse(argc, argv, f)) if (arg_parse(argc, argv, f))
return; return;
open_dev(report_opt.dev, &fd, 1, 1); open_dev(report_opt.dev, &fd, 0, 1);
get_nsid(fd, NULL, &nsid); get_nsid(fd, NULL, &nsid);
if (nsid == 0) { if (nsid == 0) {
fprintf(stderr, "This command require namespace-id\n"); fprintf(stderr, "This command require namespace-id\n");