blob: e9132b79bc515cc6df70899bbeab5517e4a55045 [file] [log] [blame]
chenh0e55d6b2014-03-27 15:19:43 -04001/*
2 * glusterfs engine
3 *
chenhcb92c7f2014-04-02 13:01:01 -04004 * common Glusterfs's gfapi interface
chenh0e55d6b2014-03-27 15:19:43 -04005 *
6 */
7
chenhcb92c7f2014-04-02 13:01:01 -04008#include "gfapi.h"
chenh0e55d6b2014-03-27 15:19:43 -04009
chenhcb92c7f2014-04-02 13:01:01 -040010struct fio_option gfapi_options[] = {
chenh0e55d6b2014-03-27 15:19:43 -040011 {
chenh321fc5a2014-03-31 11:32:29 -040012 .name = "volume",
13 .lname = "Glusterfs volume",
14 .type = FIO_OPT_STR_STORE,
15 .help = "Name of the Glusterfs volume",
16 .off1 = offsetof(struct gf_options, gf_vol),
17 .category = FIO_OPT_C_ENGINE,
18 .group = FIO_OPT_G_GFAPI,
chenh0e55d6b2014-03-27 15:19:43 -040019 },
20 {
chenh321fc5a2014-03-31 11:32:29 -040021 .name = "brick",
22 .lname = "Glusterfs brick name",
23 .type = FIO_OPT_STR_STORE,
24 .help = "Name of the Glusterfs brick to connect",
25 .off1 = offsetof(struct gf_options, gf_brick),
26 .category = FIO_OPT_C_ENGINE,
27 .group = FIO_OPT_G_GFAPI,
chenh0e55d6b2014-03-27 15:19:43 -040028 },
29 {
chenh321fc5a2014-03-31 11:32:29 -040030 .name = NULL,
chenh0e55d6b2014-03-27 15:19:43 -040031 },
32};
33
chenhcb92c7f2014-04-02 13:01:01 -040034int fio_gf_setup(struct thread_data *td)
chenh0e55d6b2014-03-27 15:19:43 -040035{
36 int r = 0;
37 struct gf_data *g = NULL;
38 struct gf_options *opt = td->eo;
chenh321fc5a2014-03-31 11:32:29 -040039 struct stat sb = {0, };
chenh0ac466f2014-03-28 15:30:54 -040040
41 dprint(FD_IO, "fio setup\n");
chenh0e55d6b2014-03-27 15:19:43 -040042
43 if (td->io_ops->data)
44 return 0;
45
46 g = malloc(sizeof(struct gf_data));
47 if (!g){
48 log_err("malloc failed.\n");
49 return -ENOMEM;
50 }
chenhcb92c7f2014-04-02 13:01:01 -040051 g->fs = NULL; g->fd = NULL; g->aio_events = NULL;
chenh0e55d6b2014-03-27 15:19:43 -040052
53 g->fs = glfs_new (opt->gf_vol);
54 if (!g->fs){
55 log_err("glfs_new failed.\n");
56 goto cleanup;
57 }
chenh0ac466f2014-03-28 15:30:54 -040058 glfs_set_logging (g->fs, "/tmp/fio_gfapi.log", 7);
chenh0e55d6b2014-03-27 15:19:43 -040059 /* default to tcp */
chenh0ac466f2014-03-28 15:30:54 -040060 r = glfs_set_volfile_server(g->fs, "tcp", opt->gf_brick, 0);
chenh0e55d6b2014-03-27 15:19:43 -040061 if (r){
62 log_err("glfs_set_volfile_server failed.\n");
63 goto cleanup;
64 }
65 r = glfs_init(g->fs);
66 if (r){
chenh0ac466f2014-03-28 15:30:54 -040067 log_err("glfs_init failed. Is glusterd running on brick?\n");
chenh0e55d6b2014-03-27 15:19:43 -040068 goto cleanup;
69 }
chenh0ac466f2014-03-28 15:30:54 -040070 sleep(2);
71 r = glfs_lstat (g->fs, ".", &sb);
72 if (r){
73 log_err("glfs_lstat failed.\n");
74 goto cleanup;
75 }
76 dprint(FD_FILE, "fio setup %p\n", g->fs);
chenh0e55d6b2014-03-27 15:19:43 -040077 td->io_ops->data = g;
78cleanup:
chenh0ac466f2014-03-28 15:30:54 -040079 if (r){
80 if (g){
chenh321fc5a2014-03-31 11:32:29 -040081 if (g->fs){
82 glfs_fini(g->fs);
83 }
84 free(g);
chenhcb92c7f2014-04-02 13:01:01 -040085 td->io_ops->data = NULL;
chenh0e55d6b2014-03-27 15:19:43 -040086 }
chenh0e55d6b2014-03-27 15:19:43 -040087 }
88 return r;
89}
90
chenhcb92c7f2014-04-02 13:01:01 -040091void fio_gf_cleanup(struct thread_data *td)
chenh0e55d6b2014-03-27 15:19:43 -040092{
chenhcb92c7f2014-04-02 13:01:01 -040093 struct gf_data *g = td->io_ops->data;
94
95 if (g) {
96 if (g->aio_events)
97 free(g->aio_events);
98 if (g->fd)
99 glfs_close(g->fd);
100 if (g->fs)
101 glfs_fini(g->fs);
102 free(g);
103 td->io_ops->data = NULL;
104 }
chenh0e55d6b2014-03-27 15:19:43 -0400105}
106
chenhcb92c7f2014-04-02 13:01:01 -0400107int fio_gf_get_file_size(struct thread_data *td, struct fio_file *f)
chenh0e55d6b2014-03-27 15:19:43 -0400108{
109 struct stat buf;
110 int ret;
111 struct gf_data *g = td->io_ops->data;
112
chenh1368c1c2014-03-28 15:01:42 -0400113 dprint(FD_FILE, "get file size %s\n", f->file_name);
114
115 if (!g || !g->fs)
116 {
chenh321fc5a2014-03-31 11:32:29 -0400117 return 0;
chenh1368c1c2014-03-28 15:01:42 -0400118 }
chenh0e55d6b2014-03-27 15:19:43 -0400119 if (fio_file_size_known(f))
chenh321fc5a2014-03-31 11:32:29 -0400120 return 0;
chenh0e55d6b2014-03-27 15:19:43 -0400121
122 ret = glfs_lstat (g->fs, f->file_name, &buf);
chenh0ac466f2014-03-28 15:30:54 -0400123 if (ret < 0){
chenh321fc5a2014-03-31 11:32:29 -0400124 log_err("glfs_lstat failed.\n");
125 return ret;
chenh0ac466f2014-03-28 15:30:54 -0400126 }
chenh0e55d6b2014-03-27 15:19:43 -0400127
128 f->real_file_size = buf.st_size;
129 fio_file_set_size_known(f);
130
131 return 0;
132
133}
134
chenhcb92c7f2014-04-02 13:01:01 -0400135int fio_gf_open_file(struct thread_data *td, struct fio_file *f)
chenh0e55d6b2014-03-27 15:19:43 -0400136{
chenh0ac466f2014-03-28 15:30:54 -0400137
chenh05c4b422014-03-28 15:07:51 -0400138 int flags = 0;
chenh0ac466f2014-03-28 15:30:54 -0400139 int ret = 0;
140 struct gf_data *g = td->io_ops->data;
chenh321fc5a2014-03-31 11:32:29 -0400141 struct stat sb = {0, };
chenh0e55d6b2014-03-27 15:19:43 -0400142
chenh0e55d6b2014-03-27 15:19:43 -0400143 if (td_write(td)) {
chenh321fc5a2014-03-31 11:32:29 -0400144 if (!read_only)
145 flags = O_RDWR;
chenh0e55d6b2014-03-27 15:19:43 -0400146 } else if (td_read(td)) {
chenh321fc5a2014-03-31 11:32:29 -0400147 if (!read_only)
148 flags = O_RDWR;
149 else
150 flags = O_RDONLY;
chenh0e55d6b2014-03-27 15:19:43 -0400151 }
chenh321fc5a2014-03-31 11:32:29 -0400152 dprint(FD_FILE, "fio file %s open mode %s td rw %s\n", f->file_name,
153 flags == O_RDONLY? "ro":"rw", td_read(td)? "read":"write");
154 g->fd = glfs_creat(g->fs, f->file_name, flags, 0644);
chenh0ac466f2014-03-28 15:30:54 -0400155 if (!g->fd){
chenh321fc5a2014-03-31 11:32:29 -0400156 log_err("glfs_creat failed.\n");
157 ret = errno;
chenh0ac466f2014-03-28 15:30:54 -0400158 }
chenh533362e2014-04-01 15:45:57 -0400159 /* file for read doesn't exist or shorter than required, create/extend it */
160 if (td_read(td)){
161 if (glfs_lstat (g->fs, f->file_name, &sb) || sb.st_size < f->real_file_size){
162 dprint(FD_FILE, "fio extend file %s from %ld to %ld\n", f->file_name, sb.st_size, f->real_file_size);
163 ret = glfs_ftruncate (g->fd, f->real_file_size);
164 if (ret){
165 log_err("failed fio extend file %s to %ld\n", f->file_name, f->real_file_size);
166 }else{
167 unsigned long long left;
168 unsigned int bs;
169 char *b;
170 int r;
171
172 /* fill the file, copied from extend_file */
173 b = malloc(td->o.max_bs[DDIR_WRITE]);
174
175 left = f->real_file_size;
176 while (left && !td->terminate) {
177 bs = td->o.max_bs[DDIR_WRITE];
178 if (bs > left)
179 bs = left;
180
181 fill_io_buffer(td, b, bs, bs);
182
183 r = glfs_write(g->fd, b, bs, 0);
184 dprint(FD_IO, "fio write %d of %ld file %s\n", r, f->real_file_size, f->file_name);
185
186 if (r > 0) {
187 left -= r;
188 continue;
189 } else {
190 if (r < 0) {
191 int __e = errno;
192
193 if (__e == ENOSPC) {
194 if (td->o.fill_device)
195 break;
196 log_info("fio: ENOSPC on laying out "
197 "file, stopping\n");
198 break;
199 }
200 td_verror(td, errno, "write");
201 } else
202 td_verror(td, EIO, "write");
203
204 break;
205 }
206 }
207
208 if (b) free(b);
209 glfs_lseek(g->fd, 0, SEEK_SET);
210
211 if (td->terminate) {
212 dprint(FD_FILE, "terminate unlink %s\n", f->file_name);
213 unlink(f->file_name);
214 } else if (td->o.create_fsync) {
215 if (glfs_fsync(g->fd) < 0) {
216 dprint(FD_FILE, "failed to sync, close %s\n", f->file_name);
217 td_verror(td, errno, "fsync");
218 glfs_close(g->fd);
219 g->fd = NULL;
220 return 1;
221 }
222 }
223 }
chenh321fc5a2014-03-31 11:32:29 -0400224 }
225 }
chenh0ac466f2014-03-28 15:30:54 -0400226 dprint(FD_FILE, "fio %p created %s\n", g->fs, f->file_name);
chenh05c4b422014-03-28 15:07:51 -0400227 f->fd = -1;
chenh0ac466f2014-03-28 15:30:54 -0400228 f->shadow_fd = -1;
229
230 return ret;
chenh0e55d6b2014-03-27 15:19:43 -0400231}
232
chenhcb92c7f2014-04-02 13:01:01 -0400233int fio_gf_close_file(struct thread_data *td, struct fio_file *f)
chenh0e55d6b2014-03-27 15:19:43 -0400234{
235 int ret = 0;
236 struct gf_data *g = td->io_ops->data;
237
238 dprint(FD_FILE, "fd close %s\n", f->file_name);
239
chenhcb92c7f2014-04-02 13:01:01 -0400240 if (g){
241 if (g->fd && glfs_close(g->fd) < 0)
242 ret = errno;
chenh0ac466f2014-03-28 15:30:54 -0400243
chenhcb92c7f2014-04-02 13:01:01 -0400244 if (g->fs)
245 glfs_fini(g->fs);
chenh0e55d6b2014-03-27 15:19:43 -0400246
chenhcb92c7f2014-04-02 13:01:01 -0400247 g->fd = NULL;
248 free(g);
249 }
chenh0ac466f2014-03-28 15:30:54 -0400250 td->io_ops->data = NULL;
chenh0e55d6b2014-03-27 15:19:43 -0400251 f->engine_data = 0;
252
253 return ret;
254}
255