Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 27 Apr 2020 14:02:24 +0000 (UTC)
From:      Alexander Motin <mav@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-12@freebsd.org
Subject:   svn commit: r360375 - stable/12/sbin/nvmecontrol
Message-ID:  <202004271402.03RE2Okl008298@repo.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: mav
Date: Mon Apr 27 14:02:24 2020
New Revision: 360375
URL: https://svnweb.freebsd.org/changeset/base/360375

Log:
  MFC r360117: Open device with O_RDONLY when command is non-invasive.
  
  This allows to use some of the subcommands against mounted nvd devices.

Modified:
  stable/12/sbin/nvmecontrol/identify.c
  stable/12/sbin/nvmecontrol/logpage.c
  stable/12/sbin/nvmecontrol/ns.c
  stable/12/sbin/nvmecontrol/nsid.c
  stable/12/sbin/nvmecontrol/nvmecontrol.c
  stable/12/sbin/nvmecontrol/nvmecontrol.h
  stable/12/sbin/nvmecontrol/resv.c
Directory Properties:
  stable/12/   (props changed)

Modified: stable/12/sbin/nvmecontrol/identify.c
==============================================================================
--- stable/12/sbin/nvmecontrol/identify.c	Mon Apr 27 13:54:00 2020	(r360374)
+++ stable/12/sbin/nvmecontrol/identify.c	Mon Apr 27 14:02:24 2020	(r360375)
@@ -241,7 +241,7 @@ identify(const struct cmd *f, int argc, char *argv[])
 	if (arg_parse(argc, argv, f))
 		return;
 
-	open_dev(opt.dev, &fd, 1, 1);
+	open_dev(opt.dev, &fd, 0, 1);
 	get_nsid(fd, &path, &nsid);
 	if (nsid != 0) {
 		/*
@@ -251,7 +251,7 @@ identify(const struct cmd *f, int argc, char *argv[])
 		 * the IDENTIFY command itself.
 		 */
 		close(fd);
-		open_dev(path, &fd, 1, 1);
+		open_dev(path, &fd, 0, 1);
 	}
 	free(path);
 	if (opt.nsid != NONE)

Modified: stable/12/sbin/nvmecontrol/logpage.c
==============================================================================
--- stable/12/sbin/nvmecontrol/logpage.c	Mon Apr 27 13:54:00 2020	(r360374)
+++ stable/12/sbin/nvmecontrol/logpage.c	Mon Apr 27 14:02:24 2020	(r360375)
@@ -687,13 +687,13 @@ logpage(const struct cmd *f, int argc, char *argv[])
 		fprintf(stderr, "Missing page_id (-p).\n");
 		arg_help(argc, argv, f);
 	}
-	open_dev(opt.dev, &fd, 1, 1);
+	open_dev(opt.dev, &fd, 0, 1);
 	get_nsid(fd, &path, &nsid);
 	if (nsid == 0) {
 		nsid = NVME_GLOBAL_NAMESPACE_TAG;
 	} else {
 		close(fd);
-		open_dev(path, &fd, 1, 1);
+		open_dev(path, &fd, 0, 1);
 	}
 	free(path);
 

Modified: stable/12/sbin/nvmecontrol/ns.c
==============================================================================
--- stable/12/sbin/nvmecontrol/ns.c	Mon Apr 27 13:54:00 2020	(r360374)
+++ stable/12/sbin/nvmecontrol/ns.c	Mon Apr 27 14:02:24 2020	(r360375)
@@ -404,7 +404,7 @@ nsactive(const struct cmd *f, int argc, char *argv[])
 
 	if (arg_parse(argc, argv, f))
 		return;
-	open_dev(active_opt.dev, &fd, 1, 1);
+	open_dev(active_opt.dev, &fd, 0, 1);
 
 	memset(&pt, 0, sizeof(pt));
 	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))
 		return;
-	open_dev(active_opt.dev, &fd, 1, 1);
+	open_dev(active_opt.dev, &fd, 0, 1);
 	read_controller_data(fd, &cd);
 
 	/* Check that controller can execute this command. */
@@ -472,7 +472,7 @@ nscontrollers(const struct cmd *f, int argc, char *arg
 
 	if (arg_parse(argc, argv, f))
 		return;
-	open_dev(controllers_opt.dev, &fd, 1, 1);
+	open_dev(controllers_opt.dev, &fd, 0, 1);
 	read_controller_data(fd, &cd);
 
 	/* 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");
 		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);
 
 	/* 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");
 		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);
 
 	/* Check that controller can execute this command. */

Modified: stable/12/sbin/nvmecontrol/nsid.c
==============================================================================
--- stable/12/sbin/nvmecontrol/nsid.c	Mon Apr 27 13:54:00 2020	(r360374)
+++ stable/12/sbin/nvmecontrol/nsid.c	Mon Apr 27 14:02:24 2020	(r360375)
@@ -73,7 +73,7 @@ gnsid(const struct cmd *f, int argc, char *argv[])
 	if (arg_parse(argc, argv, f))
 		return;
 
-	open_dev(nsid_opt.dev, &fd, 1, 1);
+	open_dev(nsid_opt.dev, &fd, 0, 1);
 	get_nsid(fd, &path, &nsid);
 	close(fd);
 	printf("%s\t%u\n", path, nsid);

Modified: stable/12/sbin/nvmecontrol/nvmecontrol.c
==============================================================================
--- stable/12/sbin/nvmecontrol/nvmecontrol.c	Mon Apr 27 13:54:00 2020	(r360374)
+++ stable/12/sbin/nvmecontrol/nvmecontrol.c	Mon Apr 27 14:02:24 2020	(r360375)
@@ -142,18 +142,17 @@ read_namespace_data(int fd, uint32_t nsid, struct nvme
 }
 
 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];
 
 	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 (show_error)
-			warn("could not open %s", full_path);
-		if (exit_on_error)
-			exit(1);
-		else
+		if (exit_on_error) {
+			err(1, "could not open %s%s", full_path,
+			    write ? " for write" : "");
+		} else
 			return (errno);
 	}
 

Modified: stable/12/sbin/nvmecontrol/nvmecontrol.h
==============================================================================
--- stable/12/sbin/nvmecontrol/nvmecontrol.h	Mon Apr 27 13:54:00 2020	(r360374)
+++ stable/12/sbin/nvmecontrol/nvmecontrol.h	Mon Apr 27 14:02:24 2020	(r360375)
@@ -68,7 +68,7 @@ void logpage_register(struct logpage_function *p);
 #define NVME_CTRLR_PREFIX	"nvme"
 #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 read_controller_data(int fd, struct nvme_controller_data *cdata);
 void read_namespace_data(int fd, uint32_t nsid, struct nvme_namespace_data *nsdata);

Modified: stable/12/sbin/nvmecontrol/resv.c
==============================================================================
--- stable/12/sbin/nvmecontrol/resv.c	Mon Apr 27 13:54:00 2020	(r360374)
+++ stable/12/sbin/nvmecontrol/resv.c	Mon Apr 27 14:02:24 2020	(r360375)
@@ -242,7 +242,7 @@ resvacquire(const struct cmd *f, int argc, char *argv[
 
 	if (arg_parse(argc, argv, f))
 		return;
-	open_dev(acquire_opt.dev, &fd, 1, 1);
+	open_dev(acquire_opt.dev, &fd, 0, 1);
 	get_nsid(fd, NULL, &nsid);
 	if (nsid == 0) {
 		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))
 		return;
-	open_dev(register_opt.dev, &fd, 1, 1);
+	open_dev(register_opt.dev, &fd, 0, 1);
 	get_nsid(fd, NULL, &nsid);
 	if (nsid == 0) {
 		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))
 		return;
-	open_dev(release_opt.dev, &fd, 1, 1);
+	open_dev(release_opt.dev, &fd, 0, 1);
 	get_nsid(fd, NULL, &nsid);
 	if (nsid == 0) {
 		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))
 		return;
-	open_dev(report_opt.dev, &fd, 1, 1);
+	open_dev(report_opt.dev, &fd, 0, 1);
 	get_nsid(fd, NULL, &nsid);
 	if (nsid == 0) {
 		fprintf(stderr, "This command require namespace-id\n");



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?202004271402.03RE2Okl008298>