fusion atomic: support for new SDK API
[fio.git] / engines / fusion-aw.c
1 /*
2  * Custom fio(1) engine that submits synchronous atomic writes to file.
3  *
4  * Copyright (C) 2013 Fusion-io, Inc.
5  * Author: Santhosh Kumar Koundinya (skoundinya@fusionio.com).
6  *
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License as published by the Free
9  * Software Foundation; under version 2 of the License.
10  *
11  * This program is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License version
14  * 2 for more details.
15  *
16  * You should have received a copy of the GNU General Public License Version 2
17  * along with this program; if not see <http://www.gnu.org/licenses/>
18  */
19
20 #include <stdlib.h>
21 #include <stdint.h>
22
23 #include "../fio.h"
24
25 #include <nvm/nvm_primitives.h>
26
27 struct fas_data {
28         nvm_handle_t nvm_handle;
29         size_t xfer_buf_align;
30         size_t xfer_buflen_align;
31         size_t xfer_buflen_max;
32         size_t sector_size;
33 };
34
35 static int queue(struct thread_data *td, struct io_u *io_u)
36 {
37         int rc;
38         struct fas_data *d = (struct fas_data *) io_u->file->engine_data;
39
40         if (io_u->ddir != DDIR_WRITE) {
41                 td_vmsg(td, EINVAL, "only writes supported", "io_u->ddir");
42                 rc = -EINVAL;
43                 goto out;
44         }
45
46         if ((size_t) io_u->xfer_buf % d->xfer_buf_align) {
47                 td_vmsg(td, EINVAL, "unaligned data buffer", "io_u->xfer_buf");
48                 rc = -EINVAL;
49                 goto out;
50         }
51
52         if (io_u->xfer_buflen % d->xfer_buflen_align) {
53                 td_vmsg(td, EINVAL, "unaligned data size", "io_u->xfer_buflen");
54                 rc = -EINVAL;
55                 goto out;
56         }
57
58         if (io_u->xfer_buflen > d->xfer_buflen_max) {
59                 td_vmsg(td, EINVAL, "data too big", "io_u->xfer_buflen");
60                 rc = -EINVAL;
61                 goto out;
62         }
63
64         rc = nvm_atomic_write(d->nvm_handle, (uint64_t) io_u->xfer_buf,
65                 io_u->xfer_buflen, io_u->offset / d->sector_size);
66         if (rc == -1) {
67                 td_verror(td, errno, "nvm_atomic_write");
68                 rc = -errno;
69                 goto out;
70         }
71         rc = FIO_Q_COMPLETED;
72 out:
73         if (rc < 0)
74                 io_u->error = -rc;
75
76         return rc;
77 }
78
79 static int open_file(struct thread_data *td, struct fio_file *f)
80 {
81         int rc;
82         int fio_unused close_file_rc;
83         struct fas_data *d;
84         nvm_version_t nvm_version;
85         nvm_capability_t nvm_capability[4];
86
87
88         d = malloc(sizeof(*d));
89         if (!d) {
90                 td_verror(td, ENOMEM, "malloc");
91                 rc = ENOMEM;
92                 goto error;
93         }
94         d->nvm_handle = -1;
95         f->engine_data = (uintptr_t) d;
96
97         rc = generic_open_file(td, f);
98
99         if (rc)
100                 goto free_engine_data;
101
102         /* Set the version of the library as seen when engine is compiled */
103         nvm_version.major = NVM_PRIMITIVES_API_MAJOR;
104         nvm_version.minor = NVM_PRIMITIVES_API_MINOR;
105         nvm_version.micro = NVM_PRIMITIVES_API_MICRO;
106
107         d->nvm_handle = nvm_get_handle(f->fd, &nvm_version);
108         if (d->nvm_handle == -1) {
109                 td_vmsg(td, errno, "nvm_get_handle failed", "nvm_get_handle");
110                 rc = errno;
111                 goto close_file;
112         }
113
114         nvm_capability[0].cap_id = NVM_CAP_ATOMIC_WRITE_START_ALIGN_ID;
115         nvm_capability[1].cap_id = NVM_CAP_ATOMIC_WRITE_MULTIPLICITY_ID;
116         nvm_capability[2].cap_id = NVM_CAP_ATOMIC_WRITE_MAX_VECTOR_SIZE_ID;
117         nvm_capability[3].cap_id = NVM_CAP_SECTOR_SIZE_ID;
118         rc = nvm_get_capabilities(d->nvm_handle, nvm_capability, 4, 0);
119         if (rc == -1) {
120                 td_vmsg(td, errno, "error in getting atomic write capabilities", "nvm_get_capabilities");
121                 rc = errno;
122                 goto close_file;
123         } else if (rc < 4) {
124                 td_vmsg(td, EINVAL, "couldn't get all the atomic write capabilities" , "nvm_get_capabilities");
125                 rc = ECANCELED;
126                 goto close_file;
127         }
128         /* Reset rc to 0 because we got all capabilities we needed */
129         rc = 0;
130         d->xfer_buf_align = nvm_capability[0].cap_value;
131         d->xfer_buflen_align = nvm_capability[1].cap_value;
132         d->xfer_buflen_max = d->xfer_buflen_align * nvm_capability[2].cap_value;
133         d->sector_size = nvm_capability[3].cap_value;
134
135 out:
136         return rc;
137 close_file:
138         close_file_rc = generic_close_file(td, f);
139 free_engine_data:
140         free(d);
141 error:
142         f->fd = -1;
143         f->engine_data = 0;
144         goto out;
145 }
146
147 static int close_file(struct thread_data *td, struct fio_file *f)
148 {
149         struct fas_data *d = (struct fas_data *) f->engine_data;
150
151         if (d) {
152                 if (d->nvm_handle != -1)
153                         nvm_release_handle(d->nvm_handle);
154                 free(d);
155                 f->engine_data = 0;
156         }
157
158         return generic_close_file(td, f);
159 }
160
161 static struct ioengine_ops ioengine = {
162         .name = "fusion-aw-sync",
163         .version = FIO_IOOPS_VERSION,
164         .queue = queue,
165         .open_file = open_file,
166         .close_file = close_file,
167         .get_file_size = generic_get_file_size,
168         .flags = FIO_SYNCIO | FIO_RAWIO | FIO_MEMALIGN
169 };
170
171 static void fio_init fio_fusion_aw_init(void)
172 {
173         register_ioengine(&ioengine);
174 }
175
176 static void fio_exit fio_fusion_aw_exit(void)
177 {
178         unregister_ioengine(&ioengine);
179 }