]> www.infradead.org Git - users/sagi/nvme-cli.git/commitdiff
NVMe: Provide easy read and write commands
authorKeith Busch <keith.busch@intel.com>
Wed, 4 Feb 2015 17:36:49 +0000 (10:36 -0700)
committerKeith Busch <keith.busch@intel.com>
Wed, 4 Feb 2015 17:36:49 +0000 (10:36 -0700)
This adds convienience functions for read and write. They are similar
to compare in that they use the helpful NVME_SUBMIT_IO IOCTL instead of
the passthru version that requires more setup.

Signed-off-by: Keith Busch <keith.busch@intel.com>
Documentation/nvme-read.1 [new file with mode: 0644]
Documentation/nvme-write.1 [new file with mode: 0644]
Documentation/nvme.1
nvme.c

diff --git a/Documentation/nvme-read.1 b/Documentation/nvme-read.1
new file mode 100644 (file)
index 0000000..abbb03b
--- /dev/null
@@ -0,0 +1,60 @@
+'\" t
+.\"     Title: nvme-compare
+.\"    Author: [FIXME: author] [see http://docbook.sf.net/el/author]
+.\" Generator: DocBook XSL Stylesheets v1.76.1 <http://docbook.sf.net/>
+.\"      Date: 02/04/2015
+.\"    Manual: \ \&
+.\"    Source: \ \&
+.\"  Language: English
+.\"
+.TH "NVME\-COMPARE" "1" "02/04/2015" "\ \&" "\ \&"
+.\" -----------------------------------------------------------------
+.\" * Define some portability stuff
+.\" -----------------------------------------------------------------
+.\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+.\" http://bugs.debian.org/507673
+.\" http://lists.gnu.org/archive/html/groff/2009-02/msg00013.html
+.\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+.ie \n(.g .ds Aq \(aq
+.el       .ds Aq '
+.\" -----------------------------------------------------------------
+.\" * set default formatting
+.\" -----------------------------------------------------------------
+.\" disable hyphenation
+.nh
+.\" disable justification (adjust text to left margin only)
+.ad l
+.\" -----------------------------------------------------------------
+.\" * MAIN CONTENT STARTS HERE *
+.\" -----------------------------------------------------------------
+.SH "NAME"
+nvme-read \- Send an NVMe Read command, provide results
+.SH "SYNOPSIS"
+.sp
+.nf
+\fInvme\-read\fR <device> [\-\-start\-block=<slba> | \-s <slba>]
+                        [\-\-block\-count=<nlb> | \-c <nlb>]
+                        [\-\-data\-size=<size> | \-z <size>]
+                        [\-\-ref\-tag=<reftag> | \-r <reftag>]
+                        [\-\-data=<data\-file> | \-d <data\-file>]
+                        [\-\-prinfo=<prinfo> | \-p <prinfo>]
+                        [\-\-app\-tag\-mask=<appmask> | \-m <appmask>]
+                        [\-\-app\-tag=<apptag> | \-a <apptag>]
+                        [\-\-limited\-retry | \-l]
+                        [\-\-force\-unit\-access | \-f]
+.fi
+.SH "DESCRIPTION"
+.sp
+The Read command reads the logical blocks specified by the command from the medium and copies to the data data buffer provided\&. Will use stdout by default if you don\(cqt provide a file\&.
+.SH "OPTIONS"
+.PP
+\-\-start\-block=<slba>, \-s <slba>
+.RS 4
+Start block\&.
+.RE
+.SH "EXAMPLES"
+.sp
+No examples yet\&.
+.SH "NVME"
+.sp
+Part of the nvme\-user suite
diff --git a/Documentation/nvme-write.1 b/Documentation/nvme-write.1
new file mode 100644 (file)
index 0000000..728b8f0
--- /dev/null
@@ -0,0 +1,60 @@
+'\" t
+.\"     Title: nvme-compare
+.\"    Author: [FIXME: author] [see http://docbook.sf.net/el/author]
+.\" Generator: DocBook XSL Stylesheets v1.76.1 <http://docbook.sf.net/>
+.\"      Date: 02/04/2015
+.\"    Manual: \ \&
+.\"    Source: \ \&
+.\"  Language: English
+.\"
+.TH "NVME\-COMPARE" "1" "02/04/2015" "\ \&" "\ \&"
+.\" -----------------------------------------------------------------
+.\" * Define some portability stuff
+.\" -----------------------------------------------------------------
+.\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+.\" http://bugs.debian.org/507673
+.\" http://lists.gnu.org/archive/html/groff/2009-02/msg00013.html
+.\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+.ie \n(.g .ds Aq \(aq
+.el       .ds Aq '
+.\" -----------------------------------------------------------------
+.\" * set default formatting
+.\" -----------------------------------------------------------------
+.\" disable hyphenation
+.nh
+.\" disable justification (adjust text to left margin only)
+.ad l
+.\" -----------------------------------------------------------------
+.\" * MAIN CONTENT STARTS HERE *
+.\" -----------------------------------------------------------------
+.SH "NAME"
+nvme-write \- Send an NVMe write command, provide results
+.SH "SYNOPSIS"
+.sp
+.nf
+\fInvme\-write\fR <device> [\-\-start\-block=<slba> | \-s <slba>]
+                        [\-\-block\-count=<nlb> | \-c <nlb>]
+                        [\-\-data\-size=<size> | \-z <size>]
+                        [\-\-ref\-tag=<reftag> | \-r <reftag>]
+                        [\-\-data=<data\-file> | \-d <data\-file>]
+                        [\-\-prinfo=<prinfo> | \-p <prinfo>]
+                        [\-\-app\-tag\-mask=<appmask> | \-m <appmask>]
+                        [\-\-app\-tag=<apptag> | \-a <apptag>]
+                        [\-\-limited\-retry | \-l]
+                        [\-\-force\-unit\-access | \-f]
+.fi
+.SH "DESCRIPTION"
+.sp
+The Write command writes the logical blocks specified by the command to the medium from the data data buffer provided\&. Will use stdin by default if you don\(cqt provide a file\&.
+.SH "OPTIONS"
+.PP
+\-\-start\-block=<slba>, \-s <slba>
+.RS 4
+Start block\&.
+.RE
+.SH "EXAMPLES"
+.sp
+No examples yet\&.
+.SH "NVME"
+.sp
+Part of the nvme\-user suite
index 3455f7f32e86343a086f476bae12ef33f85529db..7c74531ccc2af3b2563477ea214f8db043bd8cb2 100644 (file)
@@ -2,12 +2,12 @@
 .\"     Title: nvme
 .\"    Author: [see the "Authors" section]
 .\" Generator: DocBook XSL Stylesheets v1.76.1 <http://docbook.sf.net/>
-.\"      Date: 02/03/2015
+.\"      Date: 02/04/2015
 .\"    Manual: \ \&
 .\"    Source: \ \&
 .\"  Language: English
 .\"
-.TH "NVME" "1" "02/03/2015" "\ \&" "\ \&"
+.TH "NVME" "1" "02/04/2015" "\ \&" "\ \&"
 .\" -----------------------------------------------------------------
 .\" * Define some portability stuff
 .\" -----------------------------------------------------------------
@@ -44,7 +44,7 @@ This \fInvme\fR program is a user space utility to provide standards compliant t
 The utility has sub\-commands for all admin and io commands defined in the specification and for displaying controller registers\&. There is also an option to submit completely arbitrary commands\&. For a list of commands available, run "nvme help"\&.
 .SH "FURTHER DOCUMENTATION"
 .sp
-See the freely available references on \m[blue]\fBOffical NVM\-Express Site\fR\m[]\&\s-2\u[1]\d\s+2\&.
+See the freely available references on the \m[blue]\fBOffical NVM\-Express Site\fR\m[]\&\s-2\u[1]\d\s+2\&.
 .SH "AUTHORS"
 .sp
 This is written and maintained by \m[blue]\fBKeith Busch\fR\m[]\&\s-2\u[2]\d\s+2\&.
diff --git a/nvme.c b/nvme.c
index 1e16cb9d59093988c0618647fec308fc6ae13d62..cfad3bf0ad2aed11a3dc68c6987df1b3e2f00f1c 100644 (file)
--- a/nvme.c
+++ b/nvme.c
@@ -68,6 +68,8 @@ static const char *devicename;
        ENTRY(RESV_REPORT, "resv-report", "Submit a Reservation Report, return results", resv_report) \
        ENTRY(FLUSH, "flush", "Submit a Flush command, return results", flush) \
        ENTRY(COMPARE, "compare", "Submit a Comapre command, return results", compare) \
+       ENTRY(READ, "read", "Submit a read command, return results", read_cmd) \
+       ENTRY(WRITE, "write", "Submit a write command, return results", write_cmd) \
        ENTRY(REGISTERS, "show-regs", "Shows the controller registers. Requires admin character device", show_registers) \
        ENTRY(HELP, "help", "Display this help", help)
 
@@ -1671,13 +1673,16 @@ static int resv_report(int argc, char **argv)
        return 0;
 }
 
-static int compare(int argc, char **argv)
+static int submit_io(int opcode, char *command, int argc, char **argv)
 {
        struct nvme_user_io io;
        void *buffer;
-        int err, opt, dfd = STDIN_FILENO, long_index = 0;
+        int err, opt, dfd = opcode & 1 ? STDIN_FILENO : STDOUT_FILENO,
+               long_index = 0;
        unsigned int data_size = 0;
        __u8 prinfo = 0;
+
+       /* XXX: metadata ? */
        static struct option opts[] = {
                {"start-block", required_argument, 0, 's'},
                {"block-count", required_argument, 0, 'c'},
@@ -1732,21 +1737,43 @@ static int compare(int argc, char **argv)
                return EINVAL;
        }
        buffer = malloc(data_size);
-       if (read(dfd, (void *)buffer, data_size) < 0) {
-               fprintf(stderr, "failed to read compare buffer\n");
+       if ((opcode & 1) && read(dfd, (void *)buffer, data_size) < 0) {
+               fprintf(stderr, "failed to read buffer from input file\n");
                return EINVAL;
        }
 
-        io.opcode = nvme_cmd_compare;
+        io.opcode = opcode;
        io.addr = (__u64)buffer;
        err = ioctl(fd, NVME_IOCTL_SUBMIT_IO, &io);
        if (err < 0)
                perror("ioctl");
-       else
-               printf("compare:%s(%04x)\n", nvme_status_to_string(err), err);
+       else if (err)
+               printf("%s:%s(%04x)\n", command, nvme_status_to_string(err), err);
+       else {
+               if (!(opcode & 1) && write(dfd, (void *)buffer, data_size) < 0) {
+                       fprintf(stderr, "failed to write buffer to output file\n");
+                       return EINVAL;
+               } else
+                       printf("%s: success\n", command);
+       }
        return 0;
 }
 
+static int compare(int argc, char **argv)
+{
+       return submit_io(nvme_cmd_compare, "compare", argc, argv); 
+}
+
+static int read_cmd(int argc, char **argv)
+{
+       return submit_io(nvme_cmd_read, "read", argc, argv); 
+}
+
+static int write_cmd(int argc, char **argv)
+{
+       return submit_io(nvme_cmd_write, "write", argc, argv); 
+}
+
 static int sec_recv(int argc, char **argv)
 {
        struct nvme_admin_cmd cmd;