blob: 77844ffe4b724fa7420ea51f1b2c1b8d78c1caf6 [file] [log] [blame]
Santhosh Koundinya6ef63582012-09-18 23:20:21 +02001/*
2 * Custom fio(1) engine that submits synchronous atomic writes to file.
3 *
Santhosh Koundinyab2c77c22013-05-18 08:44:01 +02004 * Copyright (C) 2013 Fusion-io, Inc.
5 * Author: Santhosh Kumar Koundinya (skoundinya@fusionio.com).
Santhosh Koundinya6ef63582012-09-18 23:20:21 +02006 *
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
Jens Axboe15a6f162012-09-19 08:35:17 +02009 * Software Foundation; under version 2 of the License.
Santhosh Koundinya6ef63582012-09-18 23:20:21 +020010 *
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
Santhosh Koundinyab2c77c22013-05-18 08:44:01 +020025#include <nvm/nvm_primitives.h>
Santhosh Koundinya6ef63582012-09-18 23:20:21 +020026
Chandra Mallarapu4728b3c2013-05-21 08:08:57 +020027#define NUM_ATOMIC_CAPABILITIES (5)
28
Santhosh Koundinyab2c77c22013-05-18 08:44:01 +020029struct fas_data {
30 nvm_handle_t nvm_handle;
31 size_t xfer_buf_align;
32 size_t xfer_buflen_align;
33 size_t xfer_buflen_max;
34 size_t sector_size;
Santhosh Koundinya6ef63582012-09-18 23:20:21 +020035};
36
37static int queue(struct thread_data *td, struct io_u *io_u)
38{
Jens Axboeaa31de72014-12-15 08:26:11 -070039 struct fas_data *d = FILE_ENG_DATA(io_u->file);
Santhosh Koundinya6ef63582012-09-18 23:20:21 +020040 int rc;
Santhosh Koundinya6ef63582012-09-18 23:20:21 +020041
42 if (io_u->ddir != DDIR_WRITE) {
Santhosh Koundinyab2c77c22013-05-18 08:44:01 +020043 td_vmsg(td, EINVAL, "only writes supported", "io_u->ddir");
44 rc = -EINVAL;
Santhosh Koundinya6ef63582012-09-18 23:20:21 +020045 goto out;
46 }
47
Santhosh Koundinyab2c77c22013-05-18 08:44:01 +020048 if ((size_t) io_u->xfer_buf % d->xfer_buf_align) {
49 td_vmsg(td, EINVAL, "unaligned data buffer", "io_u->xfer_buf");
50 rc = -EINVAL;
51 goto out;
Santhosh Koundinya6ef63582012-09-18 23:20:21 +020052 }
Santhosh Koundinya6ef63582012-09-18 23:20:21 +020053
Santhosh Koundinyab2c77c22013-05-18 08:44:01 +020054 if (io_u->xfer_buflen % d->xfer_buflen_align) {
55 td_vmsg(td, EINVAL, "unaligned data size", "io_u->xfer_buflen");
56 rc = -EINVAL;
57 goto out;
58 }
59
60 if (io_u->xfer_buflen > d->xfer_buflen_max) {
61 td_vmsg(td, EINVAL, "data too big", "io_u->xfer_buflen");
62 rc = -EINVAL;
63 goto out;
64 }
65
66 rc = nvm_atomic_write(d->nvm_handle, (uint64_t) io_u->xfer_buf,
67 io_u->xfer_buflen, io_u->offset / d->sector_size);
Santhosh Koundinya6ef63582012-09-18 23:20:21 +020068 if (rc == -1) {
Santhosh Koundinyab2c77c22013-05-18 08:44:01 +020069 td_verror(td, errno, "nvm_atomic_write");
70 rc = -errno;
Santhosh Koundinya6ef63582012-09-18 23:20:21 +020071 goto out;
Santhosh Koundinya6ef63582012-09-18 23:20:21 +020072 }
Santhosh Koundinyab2c77c22013-05-18 08:44:01 +020073 rc = FIO_Q_COMPLETED;
Santhosh Koundinya6ef63582012-09-18 23:20:21 +020074out:
75 if (rc < 0)
Santhosh Koundinyab2c77c22013-05-18 08:44:01 +020076 io_u->error = -rc;
Santhosh Koundinya6ef63582012-09-18 23:20:21 +020077
78 return rc;
79}
80
81static int open_file(struct thread_data *td, struct fio_file *f)
82{
83 int rc;
Santhosh Koundinyab2c77c22013-05-18 08:44:01 +020084 int fio_unused close_file_rc;
85 struct fas_data *d;
86 nvm_version_t nvm_version;
Chandra Mallarapu4728b3c2013-05-21 08:08:57 +020087 nvm_capability_t nvm_capability[NUM_ATOMIC_CAPABILITIES];
Santhosh Koundinyab2c77c22013-05-18 08:44:01 +020088
Santhosh Koundinya6ef63582012-09-18 23:20:21 +020089
90 d = malloc(sizeof(*d));
91 if (!d) {
Santhosh Koundinyab2c77c22013-05-18 08:44:01 +020092 td_verror(td, ENOMEM, "malloc");
93 rc = ENOMEM;
Santhosh Koundinya6ef63582012-09-18 23:20:21 +020094 goto error;
95 }
Santhosh Koundinyab2c77c22013-05-18 08:44:01 +020096 d->nvm_handle = -1;
Jens Axboeaa31de72014-12-15 08:26:11 -070097 FILE_SET_ENG_DATA(f, d);
Santhosh Koundinya6ef63582012-09-18 23:20:21 +020098
99 rc = generic_open_file(td, f);
100
Santhosh Koundinyab2c77c22013-05-18 08:44:01 +0200101 if (rc)
102 goto free_engine_data;
103
104 /* Set the version of the library as seen when engine is compiled */
105 nvm_version.major = NVM_PRIMITIVES_API_MAJOR;
106 nvm_version.minor = NVM_PRIMITIVES_API_MINOR;
107 nvm_version.micro = NVM_PRIMITIVES_API_MICRO;
108
109 d->nvm_handle = nvm_get_handle(f->fd, &nvm_version);
110 if (d->nvm_handle == -1) {
111 td_vmsg(td, errno, "nvm_get_handle failed", "nvm_get_handle");
112 rc = errno;
113 goto close_file;
114 }
115
116 nvm_capability[0].cap_id = NVM_CAP_ATOMIC_WRITE_START_ALIGN_ID;
117 nvm_capability[1].cap_id = NVM_CAP_ATOMIC_WRITE_MULTIPLICITY_ID;
118 nvm_capability[2].cap_id = NVM_CAP_ATOMIC_WRITE_MAX_VECTOR_SIZE_ID;
119 nvm_capability[3].cap_id = NVM_CAP_SECTOR_SIZE_ID;
Chandra Mallarapu4728b3c2013-05-21 08:08:57 +0200120 nvm_capability[4].cap_id = NVM_CAP_ATOMIC_MAX_IOV_ID;
121 rc = nvm_get_capabilities(d->nvm_handle, nvm_capability,
122 NUM_ATOMIC_CAPABILITIES, false);
Santhosh Koundinyab2c77c22013-05-18 08:44:01 +0200123 if (rc == -1) {
124 td_vmsg(td, errno, "error in getting atomic write capabilities", "nvm_get_capabilities");
125 rc = errno;
126 goto close_file;
Chandra Mallarapu4728b3c2013-05-21 08:08:57 +0200127 } else if (rc < NUM_ATOMIC_CAPABILITIES) {
Santhosh Koundinyab2c77c22013-05-18 08:44:01 +0200128 td_vmsg(td, EINVAL, "couldn't get all the atomic write capabilities" , "nvm_get_capabilities");
129 rc = ECANCELED;
130 goto close_file;
131 }
132 /* Reset rc to 0 because we got all capabilities we needed */
133 rc = 0;
134 d->xfer_buf_align = nvm_capability[0].cap_value;
135 d->xfer_buflen_align = nvm_capability[1].cap_value;
Chandra Mallarapu4728b3c2013-05-21 08:08:57 +0200136 d->xfer_buflen_max = d->xfer_buflen_align * nvm_capability[2].cap_value * nvm_capability[4].cap_value;
Santhosh Koundinyab2c77c22013-05-18 08:44:01 +0200137 d->sector_size = nvm_capability[3].cap_value;
138
Santhosh Koundinya6ef63582012-09-18 23:20:21 +0200139out:
140 return rc;
Santhosh Koundinyab2c77c22013-05-18 08:44:01 +0200141close_file:
142 close_file_rc = generic_close_file(td, f);
143free_engine_data:
144 free(d);
Santhosh Koundinya6ef63582012-09-18 23:20:21 +0200145error:
146 f->fd = -1;
Jens Axboeaa31de72014-12-15 08:26:11 -0700147 FILE_SET_ENG_DATA(f, NULL);
Santhosh Koundinya6ef63582012-09-18 23:20:21 +0200148 goto out;
149}
150
151static int close_file(struct thread_data *td, struct fio_file *f)
152{
Jens Axboeaa31de72014-12-15 08:26:11 -0700153 struct fas_data *d = FILE_ENG_DATA(f);
Santhosh Koundinyab2c77c22013-05-18 08:44:01 +0200154
155 if (d) {
156 if (d->nvm_handle != -1)
157 nvm_release_handle(d->nvm_handle);
158 free(d);
Jens Axboeaa31de72014-12-15 08:26:11 -0700159 FILE_SET_ENG_DATA(f, NULL);
Santhosh Koundinya6ef63582012-09-18 23:20:21 +0200160 }
161
162 return generic_close_file(td, f);
163}
164
165static struct ioengine_ops ioengine = {
166 .name = "fusion-aw-sync",
167 .version = FIO_IOOPS_VERSION,
168 .queue = queue,
169 .open_file = open_file,
170 .close_file = close_file,
171 .get_file_size = generic_get_file_size,
172 .flags = FIO_SYNCIO | FIO_RAWIO | FIO_MEMALIGN
173};
174
Santhosh Koundinya6ef63582012-09-18 23:20:21 +0200175static void fio_init fio_fusion_aw_init(void)
176{
177 register_ioengine(&ioengine);
178}
179
180static void fio_exit fio_fusion_aw_exit(void)
181{
182 unregister_ioengine(&ioengine);
183}