chenh | 0e55d6b | 2014-03-27 15:19:43 -0400 | [diff] [blame] | 1 | /* |
| 2 | * glusterfs engine |
| 3 | * |
| 4 | * IO engine using Glusterfs's gfapi interface |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #include <glusterfs/api/glfs.h> |
chenh | 0ac466f | 2014-03-28 15:30:54 -0400 | [diff] [blame] | 9 | #include <glusterfs/api/glfs-handles.h> |
chenh | 0e55d6b | 2014-03-27 15:19:43 -0400 | [diff] [blame] | 10 | #include "../fio.h" |
| 11 | |
| 12 | struct gf_options { |
| 13 | struct thread_data *td; |
| 14 | char *gf_vol; |
| 15 | char *gf_brick; |
| 16 | }; |
| 17 | |
| 18 | struct gf_data { |
| 19 | glfs_t *fs; |
| 20 | glfs_fd_t *fd; |
| 21 | }; |
chenh | 0ac466f | 2014-03-28 15:30:54 -0400 | [diff] [blame] | 22 | |
chenh | 0e55d6b | 2014-03-27 15:19:43 -0400 | [diff] [blame] | 23 | static struct fio_option options[] = { |
| 24 | { |
chenh | 321fc5a | 2014-03-31 11:32:29 -0400 | [diff] [blame] | 25 | .name = "volume", |
| 26 | .lname = "Glusterfs volume", |
| 27 | .type = FIO_OPT_STR_STORE, |
| 28 | .help = "Name of the Glusterfs volume", |
| 29 | .off1 = offsetof(struct gf_options, gf_vol), |
| 30 | .category = FIO_OPT_C_ENGINE, |
| 31 | .group = FIO_OPT_G_GFAPI, |
chenh | 0e55d6b | 2014-03-27 15:19:43 -0400 | [diff] [blame] | 32 | }, |
| 33 | { |
chenh | 321fc5a | 2014-03-31 11:32:29 -0400 | [diff] [blame] | 34 | .name = "brick", |
| 35 | .lname = "Glusterfs brick name", |
| 36 | .type = FIO_OPT_STR_STORE, |
| 37 | .help = "Name of the Glusterfs brick to connect", |
| 38 | .off1 = offsetof(struct gf_options, gf_brick), |
| 39 | .category = FIO_OPT_C_ENGINE, |
| 40 | .group = FIO_OPT_G_GFAPI, |
chenh | 0e55d6b | 2014-03-27 15:19:43 -0400 | [diff] [blame] | 41 | }, |
| 42 | { |
chenh | 321fc5a | 2014-03-31 11:32:29 -0400 | [diff] [blame] | 43 | .name = NULL, |
chenh | 0e55d6b | 2014-03-27 15:19:43 -0400 | [diff] [blame] | 44 | }, |
| 45 | }; |
| 46 | |
| 47 | static int fio_gf_setup(struct thread_data *td) |
| 48 | { |
| 49 | int r = 0; |
| 50 | struct gf_data *g = NULL; |
| 51 | struct gf_options *opt = td->eo; |
chenh | 321fc5a | 2014-03-31 11:32:29 -0400 | [diff] [blame] | 52 | struct stat sb = {0, }; |
chenh | 0ac466f | 2014-03-28 15:30:54 -0400 | [diff] [blame] | 53 | |
| 54 | dprint(FD_IO, "fio setup\n"); |
chenh | 0e55d6b | 2014-03-27 15:19:43 -0400 | [diff] [blame] | 55 | |
| 56 | if (td->io_ops->data) |
| 57 | return 0; |
| 58 | |
| 59 | g = malloc(sizeof(struct gf_data)); |
| 60 | if (!g){ |
| 61 | log_err("malloc failed.\n"); |
| 62 | return -ENOMEM; |
| 63 | } |
| 64 | g->fs = NULL; g->fd = NULL; |
| 65 | |
| 66 | g->fs = glfs_new (opt->gf_vol); |
| 67 | if (!g->fs){ |
| 68 | log_err("glfs_new failed.\n"); |
| 69 | goto cleanup; |
| 70 | } |
chenh | 0ac466f | 2014-03-28 15:30:54 -0400 | [diff] [blame] | 71 | glfs_set_logging (g->fs, "/tmp/fio_gfapi.log", 7); |
chenh | 0e55d6b | 2014-03-27 15:19:43 -0400 | [diff] [blame] | 72 | /* default to tcp */ |
chenh | 0ac466f | 2014-03-28 15:30:54 -0400 | [diff] [blame] | 73 | r = glfs_set_volfile_server(g->fs, "tcp", opt->gf_brick, 0); |
chenh | 0e55d6b | 2014-03-27 15:19:43 -0400 | [diff] [blame] | 74 | if (r){ |
| 75 | log_err("glfs_set_volfile_server failed.\n"); |
| 76 | goto cleanup; |
| 77 | } |
| 78 | r = glfs_init(g->fs); |
| 79 | if (r){ |
chenh | 0ac466f | 2014-03-28 15:30:54 -0400 | [diff] [blame] | 80 | log_err("glfs_init failed. Is glusterd running on brick?\n"); |
chenh | 0e55d6b | 2014-03-27 15:19:43 -0400 | [diff] [blame] | 81 | goto cleanup; |
| 82 | } |
chenh | 0ac466f | 2014-03-28 15:30:54 -0400 | [diff] [blame] | 83 | sleep(2); |
| 84 | r = glfs_lstat (g->fs, ".", &sb); |
| 85 | if (r){ |
| 86 | log_err("glfs_lstat failed.\n"); |
| 87 | goto cleanup; |
| 88 | } |
| 89 | dprint(FD_FILE, "fio setup %p\n", g->fs); |
chenh | 0e55d6b | 2014-03-27 15:19:43 -0400 | [diff] [blame] | 90 | td->io_ops->data = g; |
| 91 | cleanup: |
chenh | 0ac466f | 2014-03-28 15:30:54 -0400 | [diff] [blame] | 92 | if (r){ |
| 93 | if (g){ |
chenh | 321fc5a | 2014-03-31 11:32:29 -0400 | [diff] [blame] | 94 | if (g->fs){ |
| 95 | glfs_fini(g->fs); |
| 96 | } |
| 97 | free(g); |
chenh | 0e55d6b | 2014-03-27 15:19:43 -0400 | [diff] [blame] | 98 | } |
chenh | 0e55d6b | 2014-03-27 15:19:43 -0400 | [diff] [blame] | 99 | } |
| 100 | return r; |
| 101 | } |
| 102 | |
| 103 | static void fio_gf_cleanup(struct thread_data *td) |
| 104 | { |
chenh | 0e55d6b | 2014-03-27 15:19:43 -0400 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | static int fio_gf_get_file_size(struct thread_data *td, struct fio_file *f) |
| 108 | { |
| 109 | struct stat buf; |
| 110 | int ret; |
| 111 | struct gf_data *g = td->io_ops->data; |
| 112 | |
chenh | 1368c1c | 2014-03-28 15:01:42 -0400 | [diff] [blame] | 113 | dprint(FD_FILE, "get file size %s\n", f->file_name); |
| 114 | |
| 115 | if (!g || !g->fs) |
| 116 | { |
chenh | 321fc5a | 2014-03-31 11:32:29 -0400 | [diff] [blame] | 117 | return 0; |
chenh | 1368c1c | 2014-03-28 15:01:42 -0400 | [diff] [blame] | 118 | } |
chenh | 0e55d6b | 2014-03-27 15:19:43 -0400 | [diff] [blame] | 119 | if (fio_file_size_known(f)) |
chenh | 321fc5a | 2014-03-31 11:32:29 -0400 | [diff] [blame] | 120 | return 0; |
chenh | 0e55d6b | 2014-03-27 15:19:43 -0400 | [diff] [blame] | 121 | |
| 122 | ret = glfs_lstat (g->fs, f->file_name, &buf); |
chenh | 0ac466f | 2014-03-28 15:30:54 -0400 | [diff] [blame] | 123 | if (ret < 0){ |
chenh | 321fc5a | 2014-03-31 11:32:29 -0400 | [diff] [blame] | 124 | log_err("glfs_lstat failed.\n"); |
| 125 | return ret; |
chenh | 0ac466f | 2014-03-28 15:30:54 -0400 | [diff] [blame] | 126 | } |
chenh | 0e55d6b | 2014-03-27 15:19:43 -0400 | [diff] [blame] | 127 | |
| 128 | f->real_file_size = buf.st_size; |
| 129 | fio_file_set_size_known(f); |
| 130 | |
| 131 | return 0; |
| 132 | |
| 133 | } |
| 134 | |
| 135 | static int fio_gf_open_file(struct thread_data *td, struct fio_file *f) |
| 136 | { |
chenh | 0ac466f | 2014-03-28 15:30:54 -0400 | [diff] [blame] | 137 | |
chenh | 05c4b42 | 2014-03-28 15:07:51 -0400 | [diff] [blame] | 138 | int flags = 0; |
chenh | 0ac466f | 2014-03-28 15:30:54 -0400 | [diff] [blame] | 139 | int ret = 0; |
| 140 | struct gf_data *g = td->io_ops->data; |
chenh | 321fc5a | 2014-03-31 11:32:29 -0400 | [diff] [blame] | 141 | struct stat sb = {0, }; |
chenh | 0e55d6b | 2014-03-27 15:19:43 -0400 | [diff] [blame] | 142 | |
chenh | 0e55d6b | 2014-03-27 15:19:43 -0400 | [diff] [blame] | 143 | if (td_write(td)) { |
chenh | 321fc5a | 2014-03-31 11:32:29 -0400 | [diff] [blame] | 144 | if (!read_only) |
| 145 | flags = O_RDWR; |
chenh | 0e55d6b | 2014-03-27 15:19:43 -0400 | [diff] [blame] | 146 | } else if (td_read(td)) { |
chenh | 321fc5a | 2014-03-31 11:32:29 -0400 | [diff] [blame] | 147 | if (!read_only) |
| 148 | flags = O_RDWR; |
| 149 | else |
| 150 | flags = O_RDONLY; |
chenh | 0e55d6b | 2014-03-27 15:19:43 -0400 | [diff] [blame] | 151 | } |
chenh | 321fc5a | 2014-03-31 11:32:29 -0400 | [diff] [blame] | 152 | 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); |
chenh | 0ac466f | 2014-03-28 15:30:54 -0400 | [diff] [blame] | 155 | if (!g->fd){ |
chenh | 321fc5a | 2014-03-31 11:32:29 -0400 | [diff] [blame] | 156 | log_err("glfs_creat failed.\n"); |
| 157 | ret = errno; |
chenh | 0ac466f | 2014-03-28 15:30:54 -0400 | [diff] [blame] | 158 | } |
chenh | 533362e | 2014-04-01 15:45:57 -0400 | [diff] [blame^] | 159 | /* 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 | } |
chenh | 321fc5a | 2014-03-31 11:32:29 -0400 | [diff] [blame] | 224 | } |
| 225 | } |
chenh | 0ac466f | 2014-03-28 15:30:54 -0400 | [diff] [blame] | 226 | dprint(FD_FILE, "fio %p created %s\n", g->fs, f->file_name); |
chenh | 05c4b42 | 2014-03-28 15:07:51 -0400 | [diff] [blame] | 227 | f->fd = -1; |
chenh | 0ac466f | 2014-03-28 15:30:54 -0400 | [diff] [blame] | 228 | f->shadow_fd = -1; |
| 229 | |
| 230 | return ret; |
chenh | 0e55d6b | 2014-03-27 15:19:43 -0400 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | static int fio_gf_close_file(struct thread_data *td, struct fio_file *f) |
| 234 | { |
| 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 | |
chenh | 0ac466f | 2014-03-28 15:30:54 -0400 | [diff] [blame] | 240 | if (g->fd && glfs_close(g->fd) < 0) |
| 241 | ret = errno; |
| 242 | |
| 243 | if (g->fs) |
| 244 | glfs_fini(g->fs); |
chenh | 0e55d6b | 2014-03-27 15:19:43 -0400 | [diff] [blame] | 245 | |
| 246 | g->fd = NULL; |
chenh | 0ac466f | 2014-03-28 15:30:54 -0400 | [diff] [blame] | 247 | free(g); |
| 248 | td->io_ops->data = NULL; |
chenh | 0e55d6b | 2014-03-27 15:19:43 -0400 | [diff] [blame] | 249 | f->engine_data = 0; |
| 250 | |
| 251 | return ret; |
| 252 | } |
| 253 | |
| 254 | #define LAST_POS(f) ((f)->engine_data) |
| 255 | static int fio_gf_prep(struct thread_data *td, struct io_u *io_u) |
| 256 | { |
| 257 | struct fio_file *f = io_u->file; |
| 258 | struct gf_data *g = td->io_ops->data; |
| 259 | |
chenh | 0ac466f | 2014-03-28 15:30:54 -0400 | [diff] [blame] | 260 | dprint(FD_FILE, "fio prep\n"); |
| 261 | |
chenh | 0e55d6b | 2014-03-27 15:19:43 -0400 | [diff] [blame] | 262 | if (!ddir_rw(io_u->ddir)) |
| 263 | return 0; |
| 264 | |
| 265 | if (LAST_POS(f) != -1ULL && LAST_POS(f) == io_u->offset) |
| 266 | return 0; |
| 267 | |
| 268 | if (glfs_lseek(g->fd, io_u->offset, SEEK_SET) < 0) { |
| 269 | td_verror(td, errno, "lseek"); |
| 270 | return 1; |
| 271 | } |
| 272 | |
| 273 | return 0; |
| 274 | } |
| 275 | |
| 276 | static int fio_gf_queue(struct thread_data *td, struct io_u *io_u) |
| 277 | { |
| 278 | struct gf_data *g = td->io_ops->data; |
| 279 | int ret = 0; |
| 280 | |
chenh | 0ac466f | 2014-03-28 15:30:54 -0400 | [diff] [blame] | 281 | dprint(FD_FILE, "fio queue len %lu\n", io_u->xfer_buflen); |
chenh | 0e55d6b | 2014-03-27 15:19:43 -0400 | [diff] [blame] | 282 | fio_ro_check(td, io_u); |
| 283 | |
| 284 | if (io_u->ddir == DDIR_READ) |
chenh | 321fc5a | 2014-03-31 11:32:29 -0400 | [diff] [blame] | 285 | ret = glfs_read(g->fd, io_u->xfer_buf, io_u->xfer_buflen, 0); |
chenh | 0e55d6b | 2014-03-27 15:19:43 -0400 | [diff] [blame] | 286 | else if (io_u->ddir == DDIR_WRITE) |
chenh | 321fc5a | 2014-03-31 11:32:29 -0400 | [diff] [blame] | 287 | ret = glfs_write(g->fd, io_u->xfer_buf, io_u->xfer_buflen, 0); |
chenh | 0e55d6b | 2014-03-27 15:19:43 -0400 | [diff] [blame] | 288 | else { |
chenh | 321fc5a | 2014-03-31 11:32:29 -0400 | [diff] [blame] | 289 | log_err("unsupported operation.\n"); |
| 290 | return -EINVAL; |
chenh | 0e55d6b | 2014-03-27 15:19:43 -0400 | [diff] [blame] | 291 | } |
chenh | 321fc5a | 2014-03-31 11:32:29 -0400 | [diff] [blame] | 292 | dprint(FD_FILE, "fio len %lu ret %d\n", io_u->xfer_buflen, ret); |
chenh | 0e55d6b | 2014-03-27 15:19:43 -0400 | [diff] [blame] | 293 | if (io_u->file && ret >= 0 && ddir_rw(io_u->ddir)) |
chenh | 321fc5a | 2014-03-31 11:32:29 -0400 | [diff] [blame] | 294 | LAST_POS(io_u->file) = io_u->offset + ret; |
chenh | 0e55d6b | 2014-03-27 15:19:43 -0400 | [diff] [blame] | 295 | |
| 296 | if (ret != (int) io_u->xfer_buflen) { |
chenh | 321fc5a | 2014-03-31 11:32:29 -0400 | [diff] [blame] | 297 | if (ret >= 0) { |
| 298 | io_u->resid = io_u->xfer_buflen - ret; |
| 299 | io_u->error = 0; |
| 300 | return FIO_Q_COMPLETED; |
| 301 | } else |
| 302 | io_u->error = errno; |
chenh | 0e55d6b | 2014-03-27 15:19:43 -0400 | [diff] [blame] | 303 | } |
| 304 | |
chenh | 0ac466f | 2014-03-28 15:30:54 -0400 | [diff] [blame] | 305 | if (io_u->error){ |
chenh | 321fc5a | 2014-03-31 11:32:29 -0400 | [diff] [blame] | 306 | log_err("IO failed.\n"); |
| 307 | td_verror(td, io_u->error, "xfer"); |
chenh | 0ac466f | 2014-03-28 15:30:54 -0400 | [diff] [blame] | 308 | } |
chenh | 0e55d6b | 2014-03-27 15:19:43 -0400 | [diff] [blame] | 309 | |
| 310 | return FIO_Q_COMPLETED; |
| 311 | |
| 312 | } |
| 313 | |
| 314 | static struct ioengine_ops ioengine = { |
| 315 | .name = "gfapi", |
| 316 | .version = FIO_IOOPS_VERSION, |
chenh | 321fc5a | 2014-03-31 11:32:29 -0400 | [diff] [blame] | 317 | .init = fio_gf_setup, |
| 318 | .cleanup = fio_gf_cleanup, |
chenh | 0e55d6b | 2014-03-27 15:19:43 -0400 | [diff] [blame] | 319 | .prep = fio_gf_prep, |
| 320 | .queue = fio_gf_queue, |
| 321 | .open_file = fio_gf_open_file, |
| 322 | .close_file = fio_gf_close_file, |
chenh | 321fc5a | 2014-03-31 11:32:29 -0400 | [diff] [blame] | 323 | .get_file_size = fio_gf_get_file_size, |
| 324 | .options = options, |
chenh | 0e55d6b | 2014-03-27 15:19:43 -0400 | [diff] [blame] | 325 | .option_struct_size = sizeof(struct gf_options), |
chenh | 0ac466f | 2014-03-28 15:30:54 -0400 | [diff] [blame] | 326 | .flags = FIO_SYNCIO | FIO_DISKLESSIO, |
chenh | 0e55d6b | 2014-03-27 15:19:43 -0400 | [diff] [blame] | 327 | }; |
| 328 | |
| 329 | static void fio_init fio_gf_register(void) |
| 330 | { |
| 331 | register_ioengine(&ioengine); |
| 332 | } |
| 333 | |
| 334 | static void fio_exit fio_gf_unregister(void) |
| 335 | { |
| 336 | unregister_ioengine(&ioengine); |
| 337 | } |