From: Daniel Wagner Date: Tue, 15 Feb 2022 14:26:37 +0000 (+0100) Subject: nvme: Only open device once X-Git-Tag: v2.0-rc4~11^2~2 X-Git-Url: https://www.infradead.org/git/?a=commitdiff_plain;h=a78869527956eb6045f32a051239f92c55a471ba;p=users%2Fsagi%2Fnvme-cli.git nvme: Only open device once 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 --- diff --git a/nvme.c b/nvme.c index f92fc93f..35e0b121 100644 --- 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");