]> www.infradead.org Git - users/sagi/nvme-cli.git/commitdiff
nvme: Fix error when passthru does not match opcode based flags
authorSteven Seungcheol Lee <sc108.lee@samsung.com>
Tue, 12 Apr 2022 05:45:46 +0000 (14:45 +0900)
committerSteven Seungcheol Lee <sc108.lee@samsung.com>
Fri, 22 Apr 2022 03:05:44 +0000 (12:05 +0900)
when write option is set(but opcode is not), file should open with read flag
same for read
it occur 'Bad file descriptor' error when access the file

write & read flags are set by opcode as well
Spec mentioned below about opcode(01:00 Data Transfer)
All options to the command shall transfer data as specified or transfer no data.
All commands, including vendor specific commands
01b = host to controller; 10b = controller to host

Reported-by: Kyungsik Shin <ks1012.shin@samsung.com>
Signed-off-by: Steven Seungcheol Lee <sc108.lee@samsung.com>
nvme.c

diff --git a/nvme.c b/nvme.c
index 4533413487342c5bda526ba6011dcdc1e9ecc8a8..c75bf741ca72d4bf8c8abda95e2c4f33555a2dc3 100644 (file)
--- a/nvme.c
+++ b/nvme.c
@@ -7313,8 +7313,22 @@ static int passthru(int argc, char **argv, bool admin,
        if (fd < 0)
                goto ret;
 
-       flags = cfg.opcode & 1 ? O_RDONLY : O_WRONLY | O_CREAT;
-       dfd = mfd = cfg.opcode & 1 ? STDIN_FILENO : STDOUT_FILENO;
+       if (cfg.opcode & 0x01)
+               cfg.write = true;
+
+       if (cfg.opcode & 0x02)
+               cfg.read = true;
+
+       if (cfg.write) {
+               flags = O_RDONLY;
+               dfd = mfd = STDIN_FILENO;
+       }
+
+       if (cfg.read) {
+               flags = O_WRONLY | O_CREAT;
+               dfd = mfd = STDOUT_FILENO;
+       }
+
        if (strlen(cfg.input_file)) {
                dfd = open(cfg.input_file, flags, mode);
                if (dfd < 0) {
@@ -7357,14 +7371,6 @@ static int passthru(int argc, char **argv, bool admin,
                        goto free_metadata;
                }
 
-               if (cfg.write && !(cfg.opcode & 0x01)) {
-                       fprintf(stderr, "warning: write flag set but write direction bit is not set in the opcode\n");
-               }
-
-               if (cfg.read && !(cfg.opcode & 0x02)) {
-                       fprintf(stderr, "warning: read flag set but read direction bit is not set in the opcode\n");
-               }
-
                memset(data, cfg.prefill, cfg.data_len);
                if (!cfg.read && !cfg.write) {
                        fprintf(stderr, "data direction not given\n");