]> www.infradead.org Git - users/sagi/nvme-cli.git/commitdiff
nvme: Only open device once
authorDaniel Wagner <dwagner@suse.de>
Tue, 15 Feb 2022 14:26:37 +0000 (15:26 +0100)
committerDaniel Wagner <dwagner@suse.de>
Tue, 15 Feb 2022 14:31:41 +0000 (15:31 +0100)
open_exclusive() takes care with opening the device. Do not open a
second time the device.

open_exclusive() has an overwrite flag. Document it a bit better by
renaming it what it actually means. The idea here is that the device
is openened with O_EXCL and when forced is used, open without this
flag.

Signed-off-by: Daniel Wagner <dwagner@suse.de>
nvme.c

diff --git a/nvme.c b/nvme.c
index f92fc93fb44a75df9185b655cb1809b28b4bba1d..35e0b121b20fc9fb53413eb940f76c9b7bf380e2 100644 (file)
--- a/nvme.c
+++ b/nvme.c
@@ -280,11 +280,11 @@ int parse_and_open(int argc, char **argv, const char *desc,
        return ret;
 }
 
-int open_exclusive(int argc, char **argv, int force)
+int open_exclusive(int argc, char **argv, int ignore_exclusive)
 {
     int flags = O_RDONLY;
 
-       if (!force)
+       if (!ignore_exclusive)
                flags |= O_EXCL;
 
     return get_dev(argc, argv, flags);
@@ -4519,9 +4519,11 @@ static int format(int argc, char **argv, struct command *cmd, struct plugin *plu
        if (fd < 0) {
                if (errno == EBUSY) {
                        fprintf(stderr, "Failed to open %s.\n",
-                basename(argv[optind]));
+                               basename(argv[optind]));
                        fprintf(stderr,
-                               "Namespace is currently busy.\n"
+                               "Namespace is currently busy.\n");
+                       if (!cfg.force)
+                               fprintf(stderr,
                                "Use the force [--force|-f] option to ignore that.\n");
                } else {
                        argconfig_print_help(desc, opts);
@@ -4529,10 +4531,6 @@ static int format(int argc, char **argv, struct command *cmd, struct plugin *plu
                goto ret;
        }
 
-       err = fd = parse_and_open(argc, argv, desc, opts);
-       if (fd < 0)
-               goto ret;
-
        if (cfg.lbaf != 0xff && cfg.bs !=0) {
                fprintf(stderr,
                        "Invalid specification of both LBAF and Block Size, please specify only one\n");