fusion atomic: support for new SDK API
[fio.git] / engines / fusion-aw.c
CommitLineData
6ef63580
SK
1/*
2 * Custom fio(1) engine that submits synchronous atomic writes to file.
3 *
b2c77c25
SK
4 * Copyright (C) 2013 Fusion-io, Inc.
5 * Author: Santhosh Kumar Koundinya (skoundinya@fusionio.com).
6ef63580
SK
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
15a6f166 9 * Software Foundation; under version 2 of the License.
6ef63580
SK
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
b2c77c25 25#include <nvm/nvm_primitives.h>
6ef63580 26
b2c77c25
SK
27struct 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;
6ef63580
SK
33};
34
35static int queue(struct thread_data *td, struct io_u *io_u)
36{
37 int rc;
b2c77c25 38 struct fas_data *d = (struct fas_data *) io_u->file->engine_data;
6ef63580
SK
39
40 if (io_u->ddir != DDIR_WRITE) {
b2c77c25
SK
41 td_vmsg(td, EINVAL, "only writes supported", "io_u->ddir");
42 rc = -EINVAL;
6ef63580
SK
43 goto out;
44 }
b2c77c25
SK
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;
6ef63580
SK
49 goto out;
50 }
b2c77c25
SK
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;
6ef63580
SK
55 goto out;
56 }
57
b2c77c25
SK
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;
6ef63580 62 }
6ef63580 63
b2c77c25
SK
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);
6ef63580 66 if (rc == -1) {
b2c77c25
SK
67 td_verror(td, errno, "nvm_atomic_write");
68 rc = -errno;
6ef63580 69 goto out;
6ef63580 70 }
b2c77c25 71 rc = FIO_Q_COMPLETED;
6ef63580
SK
72out:
73 if (rc < 0)
b2c77c25 74 io_u->error = -rc;
6ef63580
SK
75
76 return rc;
77}
78
79static int open_file(struct thread_data *td, struct fio_file *f)
80{
81 int rc;
b2c77c25
SK
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
6ef63580
SK
87
88 d = malloc(sizeof(*d));
89 if (!d) {
b2c77c25
SK
90 td_verror(td, ENOMEM, "malloc");
91 rc = ENOMEM;
6ef63580
SK
92 goto error;
93 }
b2c77c25 94 d->nvm_handle = -1;
c11a9ddf 95 f->engine_data = (uintptr_t) d;
6ef63580
SK
96
97 rc = generic_open_file(td, f);
98
b2c77c25
SK
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
6ef63580
SK
135out:
136 return rc;
b2c77c25
SK
137close_file:
138 close_file_rc = generic_close_file(td, f);
139free_engine_data:
140 free(d);
6ef63580
SK
141error:
142 f->fd = -1;
c11a9ddf 143 f->engine_data = 0;
6ef63580
SK
144 goto out;
145}
146
147static int close_file(struct thread_data *td, struct fio_file *f)
148{
b2c77c25
SK
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);
c11a9ddf 155 f->engine_data = 0;
6ef63580
SK
156 }
157
158 return generic_close_file(td, f);
159}
160
161static 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
6ef63580
SK
171static void fio_init fio_fusion_aw_init(void)
172{
173 register_ioengine(&ioengine);
174}
175
176static void fio_exit fio_fusion_aw_exit(void)
177{
178 unregister_ioengine(&ioengine);
179}