chenh | 0e55d6b | 2014-03-27 15:19:43 -0400 | [diff] [blame] | 1 | /* |
| 2 | * glusterfs engine |
| 3 | * |
chenh | cb92c7f | 2014-04-02 13:01:01 -0400 | [diff] [blame^] | 4 | * common Glusterfs's gfapi interface |
chenh | 0e55d6b | 2014-03-27 15:19:43 -0400 | [diff] [blame] | 5 | * |
| 6 | */ |
| 7 | |
chenh | cb92c7f | 2014-04-02 13:01:01 -0400 | [diff] [blame^] | 8 | #include "gfapi.h" |
chenh | 0e55d6b | 2014-03-27 15:19:43 -0400 | [diff] [blame] | 9 | |
chenh | cb92c7f | 2014-04-02 13:01:01 -0400 | [diff] [blame^] | 10 | struct fio_option gfapi_options[] = { |
chenh | 0e55d6b | 2014-03-27 15:19:43 -0400 | [diff] [blame] | 11 | { |
chenh | 321fc5a | 2014-03-31 11:32:29 -0400 | [diff] [blame] | 12 | .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, |
chenh | 0e55d6b | 2014-03-27 15:19:43 -0400 | [diff] [blame] | 19 | }, |
| 20 | { |
chenh | 321fc5a | 2014-03-31 11:32:29 -0400 | [diff] [blame] | 21 | .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, |
chenh | 0e55d6b | 2014-03-27 15:19:43 -0400 | [diff] [blame] | 28 | }, |
| 29 | { |
chenh | 321fc5a | 2014-03-31 11:32:29 -0400 | [diff] [blame] | 30 | .name = NULL, |
chenh | 0e55d6b | 2014-03-27 15:19:43 -0400 | [diff] [blame] | 31 | }, |
| 32 | }; |
| 33 | |
chenh | cb92c7f | 2014-04-02 13:01:01 -0400 | [diff] [blame^] | 34 | int fio_gf_setup(struct thread_data *td) |
chenh | 0e55d6b | 2014-03-27 15:19:43 -0400 | [diff] [blame] | 35 | { |
| 36 | int r = 0; |
| 37 | struct gf_data *g = NULL; |
| 38 | struct gf_options *opt = td->eo; |
chenh | 321fc5a | 2014-03-31 11:32:29 -0400 | [diff] [blame] | 39 | struct stat sb = {0, }; |
chenh | 0ac466f | 2014-03-28 15:30:54 -0400 | [diff] [blame] | 40 | |
| 41 | dprint(FD_IO, "fio setup\n"); |
chenh | 0e55d6b | 2014-03-27 15:19:43 -0400 | [diff] [blame] | 42 | |
| 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 | } |
chenh | cb92c7f | 2014-04-02 13:01:01 -0400 | [diff] [blame^] | 51 | g->fs = NULL; g->fd = NULL; g->aio_events = NULL; |
chenh | 0e55d6b | 2014-03-27 15:19:43 -0400 | [diff] [blame] | 52 | |
| 53 | g->fs = glfs_new (opt->gf_vol); |
| 54 | if (!g->fs){ |
| 55 | log_err("glfs_new failed.\n"); |
| 56 | goto cleanup; |
| 57 | } |
chenh | 0ac466f | 2014-03-28 15:30:54 -0400 | [diff] [blame] | 58 | glfs_set_logging (g->fs, "/tmp/fio_gfapi.log", 7); |
chenh | 0e55d6b | 2014-03-27 15:19:43 -0400 | [diff] [blame] | 59 | /* default to tcp */ |
chenh | 0ac466f | 2014-03-28 15:30:54 -0400 | [diff] [blame] | 60 | r = glfs_set_volfile_server(g->fs, "tcp", opt->gf_brick, 0); |
chenh | 0e55d6b | 2014-03-27 15:19:43 -0400 | [diff] [blame] | 61 | if (r){ |
| 62 | log_err("glfs_set_volfile_server failed.\n"); |
| 63 | goto cleanup; |
| 64 | } |
| 65 | r = glfs_init(g->fs); |
| 66 | if (r){ |
chenh | 0ac466f | 2014-03-28 15:30:54 -0400 | [diff] [blame] | 67 | log_err("glfs_init failed. Is glusterd running on brick?\n"); |
chenh | 0e55d6b | 2014-03-27 15:19:43 -0400 | [diff] [blame] | 68 | goto cleanup; |
| 69 | } |
chenh | 0ac466f | 2014-03-28 15:30:54 -0400 | [diff] [blame] | 70 | 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); |
chenh | 0e55d6b | 2014-03-27 15:19:43 -0400 | [diff] [blame] | 77 | td->io_ops->data = g; |
| 78 | cleanup: |
chenh | 0ac466f | 2014-03-28 15:30:54 -0400 | [diff] [blame] | 79 | if (r){ |
| 80 | if (g){ |
chenh | 321fc5a | 2014-03-31 11:32:29 -0400 | [diff] [blame] | 81 | if (g->fs){ |
| 82 | glfs_fini(g->fs); |
| 83 | } |
| 84 | free(g); |
chenh | cb92c7f | 2014-04-02 13:01:01 -0400 | [diff] [blame^] | 85 | td->io_ops->data = NULL; |
chenh | 0e55d6b | 2014-03-27 15:19:43 -0400 | [diff] [blame] | 86 | } |
chenh | 0e55d6b | 2014-03-27 15:19:43 -0400 | [diff] [blame] | 87 | } |
| 88 | return r; |
| 89 | } |
| 90 | |
chenh | cb92c7f | 2014-04-02 13:01:01 -0400 | [diff] [blame^] | 91 | void fio_gf_cleanup(struct thread_data *td) |
chenh | 0e55d6b | 2014-03-27 15:19:43 -0400 | [diff] [blame] | 92 | { |
chenh | cb92c7f | 2014-04-02 13:01:01 -0400 | [diff] [blame^] | 93 | 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 | } |
chenh | 0e55d6b | 2014-03-27 15:19:43 -0400 | [diff] [blame] | 105 | } |
| 106 | |
chenh | cb92c7f | 2014-04-02 13:01:01 -0400 | [diff] [blame^] | 107 | int fio_gf_get_file_size(struct thread_data *td, struct fio_file *f) |
chenh | 0e55d6b | 2014-03-27 15:19:43 -0400 | [diff] [blame] | 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 | |
chenh | cb92c7f | 2014-04-02 13:01:01 -0400 | [diff] [blame^] | 135 | int fio_gf_open_file(struct thread_data *td, struct fio_file *f) |
chenh | 0e55d6b | 2014-03-27 15:19:43 -0400 | [diff] [blame] | 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 | |
chenh | cb92c7f | 2014-04-02 13:01:01 -0400 | [diff] [blame^] | 233 | int fio_gf_close_file(struct thread_data *td, struct fio_file *f) |
chenh | 0e55d6b | 2014-03-27 15:19:43 -0400 | [diff] [blame] | 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 | cb92c7f | 2014-04-02 13:01:01 -0400 | [diff] [blame^] | 240 | if (g){ |
| 241 | if (g->fd && glfs_close(g->fd) < 0) |
| 242 | ret = errno; |
chenh | 0ac466f | 2014-03-28 15:30:54 -0400 | [diff] [blame] | 243 | |
chenh | cb92c7f | 2014-04-02 13:01:01 -0400 | [diff] [blame^] | 244 | if (g->fs) |
| 245 | glfs_fini(g->fs); |
chenh | 0e55d6b | 2014-03-27 15:19:43 -0400 | [diff] [blame] | 246 | |
chenh | cb92c7f | 2014-04-02 13:01:01 -0400 | [diff] [blame^] | 247 | g->fd = NULL; |
| 248 | free(g); |
| 249 | } |
chenh | 0ac466f | 2014-03-28 15:30:54 -0400 | [diff] [blame] | 250 | td->io_ops->data = NULL; |
chenh | 0e55d6b | 2014-03-27 15:19:43 -0400 | [diff] [blame] | 251 | f->engine_data = 0; |
| 252 | |
| 253 | return ret; |
| 254 | } |
| 255 | |