From: Daniel Wagner Date: Tue, 15 Feb 2022 15:00:53 +0000 (+0100) Subject: nvme: Open device exclusive for write command X-Git-Tag: v2.0-rc4~11^2~1 X-Git-Url: https://www.infradead.org/git/?a=commitdiff_plain;h=0adb8c2c79df953bbef9e08ce684239a5650638f;p=users%2Fsagi%2Fnvme-cli.git nvme: Open device exclusive for write command Commit 2100609ddefb ("nvme: open namespace exclusive by default") tried to make the write command a bit safer by asking to open the device exclusive. Because the commit got reverted let's add the main change back which is to use O_EXCL for write commands on default. Signed-off-by: Daniel Wagner --- diff --git a/nvme.c b/nvme.c index 35e0b121..9b5b6d15 100644 --- a/nvme.c +++ b/nvme.c @@ -6059,6 +6059,7 @@ static int submit_io(int opcode, char *command, const char *desc, "checked as part of end-to-end data protection processing"; const char *storage_tag = "storage tag, CDW2 and CDW3 (00:47) bits "\ "(for end to end PI)"; + const char *force = "The \"I know what I'm doing\" flag, do not enforce exclusive access for write"; struct config { __u32 namespace_id; @@ -6082,6 +6083,7 @@ static int submit_io(int opcode, char *command, const char *desc, int show; int dry_run; int latency; + int force; }; struct config cfg = { @@ -6098,6 +6100,7 @@ static int submit_io(int opcode, char *command, const char *desc, .app_tag = 0, .storage_tag_check = 0, .storage_tag = 0, + .force = 0, }; OPT_ARGS(opts) = { @@ -6122,12 +6125,28 @@ static int submit_io(int opcode, char *command, const char *desc, OPT_FLAG("show-command", 'v', &cfg.show, show), OPT_FLAG("dry-run", 'w', &cfg.dry_run, dry), OPT_FLAG("latency", 't', &cfg.latency, latency), + OPT_FLAG("force", 0, &cfg.force, force), OPT_END() }; - err = fd = parse_and_open(argc, argv, desc, opts); - if (fd < 0) - goto ret; + if (opcode != nvme_cmd_write) { + err = fd = parse_and_open(argc, argv, desc, opts); + if (fd < 0) + goto ret; + } else { + err = fd = open_exclusive(argc, argv, cfg.force); + if (errno == EBUSY) { + fprintf(stderr, "Failed to open %s.\n", + basename(argv[optind])); + fprintf(stderr, + "Namespace is currently busy.\n"); + if (!cfg.force) + fprintf(stderr, + "Use the force [--force] option to ignore that.\n"); + } else { + argconfig_print_help(desc, opts); + } + } if (!cfg.namespace_id) { err = nvme_get_nsid(fd, &cfg.namespace_id);