Fix segmentation fault while specifying clustername with rbd engine
[fio.git] / engines / rbd.c
1 /*
2  * rbd engine
3  *
4  * IO engine using Ceph's librbd to test RADOS Block Devices.
5  *
6  */
7
8 #include <rbd/librbd.h>
9
10 #include "../fio.h"
11 #include "../optgroup.h"
12
13 struct fio_rbd_iou {
14         struct io_u *io_u;
15         rbd_completion_t completion;
16         int io_seen;
17         int io_complete;
18 };
19
20 struct rbd_data {
21         rados_t cluster;
22         rados_ioctx_t io_ctx;
23         rbd_image_t image;
24         struct io_u **aio_events;
25         struct io_u **sort_events;
26 };
27
28 struct rbd_options {
29         void *pad;
30         char *cluster_name;
31         char *rbd_name;
32         char *pool_name;
33         char *client_name;
34         int busy_poll;
35 };
36
37 static struct fio_option options[] = {
38         {
39                 .name           = "clustername",
40                 .lname          = "ceph cluster name",
41                 .type           = FIO_OPT_STR_STORE,
42                 .help           = "Cluster name for ceph",
43                 .off1           = offsetof(struct rbd_options, cluster_name),
44                 .category       = FIO_OPT_C_ENGINE,
45                 .group          = FIO_OPT_G_RBD,
46         },
47         {
48                 .name           = "rbdname",
49                 .lname          = "rbd engine rbdname",
50                 .type           = FIO_OPT_STR_STORE,
51                 .help           = "RBD name for RBD engine",
52                 .off1           = offsetof(struct rbd_options, rbd_name),
53                 .category       = FIO_OPT_C_ENGINE,
54                 .group          = FIO_OPT_G_RBD,
55         },
56         {
57                 .name           = "pool",
58                 .lname          = "rbd engine pool",
59                 .type           = FIO_OPT_STR_STORE,
60                 .help           = "Name of the pool hosting the RBD for the RBD engine",
61                 .off1           = offsetof(struct rbd_options, pool_name),
62                 .category       = FIO_OPT_C_ENGINE,
63                 .group          = FIO_OPT_G_RBD,
64         },
65         {
66                 .name           = "clientname",
67                 .lname          = "rbd engine clientname",
68                 .type           = FIO_OPT_STR_STORE,
69                 .help           = "Name of the ceph client to access the RBD for the RBD engine",
70                 .off1           = offsetof(struct rbd_options, client_name),
71                 .category       = FIO_OPT_C_ENGINE,
72                 .group          = FIO_OPT_G_RBD,
73         },
74         {
75                 .name           = "busy_poll",
76                 .lname          = "Busy poll",
77                 .type           = FIO_OPT_BOOL,
78                 .help           = "Busy poll for completions instead of sleeping",
79                 .off1           = offsetof(struct rbd_options, busy_poll),
80                 .def            = "0",
81                 .category       = FIO_OPT_C_ENGINE,
82                 .group          = FIO_OPT_G_RBD,
83         },
84         {
85                 .name = NULL,
86         },
87 };
88
89 static int _fio_setup_rbd_data(struct thread_data *td,
90                                struct rbd_data **rbd_data_ptr)
91 {
92         struct rbd_data *rbd;
93
94         if (td->io_ops_data)
95                 return 0;
96
97         rbd = calloc(1, sizeof(struct rbd_data));
98         if (!rbd)
99                 goto failed;
100
101         rbd->aio_events = calloc(td->o.iodepth, sizeof(struct io_u *));
102         if (!rbd->aio_events)
103                 goto failed;
104
105         rbd->sort_events = calloc(td->o.iodepth, sizeof(struct io_u *));
106         if (!rbd->sort_events)
107                 goto failed;
108
109         *rbd_data_ptr = rbd;
110         return 0;
111
112 failed:
113         if (rbd) {
114                 if (rbd->aio_events) 
115                         free(rbd->aio_events);
116                 if (rbd->sort_events)
117                         free(rbd->sort_events);
118                 free(rbd);
119         }
120         return 1;
121
122 }
123
124 static int _fio_rbd_connect(struct thread_data *td)
125 {
126         struct rbd_data *rbd = td->io_ops_data;
127         struct rbd_options *o = td->eo;
128         int r;
129
130         if (o->cluster_name) {
131                 char *client_name = NULL; 
132
133                 /*
134                  * If we specify cluser name, the rados_create2
135                  * will not assume 'client.'. name is considered
136                  * as a full type.id namestr
137                  */
138                 if (o->client_name) {
139                         if (!index(o->client_name, '.')) {
140                                 client_name = calloc(1, strlen("client.") +
141                                                     strlen(o->client_name) + 1);
142                                 strcat(client_name, "client.");
143                                 o->client_name = strcat(client_name,
144                                                         o->client_name);
145                         }
146                 }
147                 r = rados_create2(&rbd->cluster, o->cluster_name,
148                                         o->client_name, 0);
149         } else
150                 r = rados_create(&rbd->cluster, o->client_name);
151         
152         if (r < 0) {
153                 log_err("rados_create failed.\n");
154                 goto failed_early;
155         }
156
157         r = rados_conf_read_file(rbd->cluster, NULL);
158         if (r < 0) {
159                 log_err("rados_conf_read_file failed.\n");
160                 goto failed_early;
161         }
162
163         r = rados_connect(rbd->cluster);
164         if (r < 0) {
165                 log_err("rados_connect failed.\n");
166                 goto failed_shutdown;
167         }
168
169         r = rados_ioctx_create(rbd->cluster, o->pool_name, &rbd->io_ctx);
170         if (r < 0) {
171                 log_err("rados_ioctx_create failed.\n");
172                 goto failed_shutdown;
173         }
174
175         r = rbd_open(rbd->io_ctx, o->rbd_name, &rbd->image, NULL /*snap */ );
176         if (r < 0) {
177                 log_err("rbd_open failed.\n");
178                 goto failed_open;
179         }
180         return 0;
181
182 failed_open:
183         rados_ioctx_destroy(rbd->io_ctx);
184         rbd->io_ctx = NULL;
185 failed_shutdown:
186         rados_shutdown(rbd->cluster);
187         rbd->cluster = NULL;
188 failed_early:
189         return 1;
190 }
191
192 static void _fio_rbd_disconnect(struct rbd_data *rbd)
193 {
194         if (!rbd)
195                 return;
196
197         /* shutdown everything */
198         if (rbd->image) {
199                 rbd_close(rbd->image);
200                 rbd->image = NULL;
201         }
202
203         if (rbd->io_ctx) {
204                 rados_ioctx_destroy(rbd->io_ctx);
205                 rbd->io_ctx = NULL;
206         }
207
208         if (rbd->cluster) {
209                 rados_shutdown(rbd->cluster);
210                 rbd->cluster = NULL;
211         }
212 }
213
214 static void _fio_rbd_finish_aiocb(rbd_completion_t comp, void *data)
215 {
216         struct fio_rbd_iou *fri = data;
217         struct io_u *io_u = fri->io_u;
218         ssize_t ret;
219
220         /*
221          * Looks like return value is 0 for success, or < 0 for
222          * a specific error. So we have to assume that it can't do
223          * partial completions.
224          */
225         ret = rbd_aio_get_return_value(fri->completion);
226         if (ret < 0) {
227                 io_u->error = ret;
228                 io_u->resid = io_u->xfer_buflen;
229         } else
230                 io_u->error = 0;
231
232         fri->io_complete = 1;
233 }
234
235 static struct io_u *fio_rbd_event(struct thread_data *td, int event)
236 {
237         struct rbd_data *rbd = td->io_ops_data;
238
239         return rbd->aio_events[event];
240 }
241
242 static inline int fri_check_complete(struct rbd_data *rbd, struct io_u *io_u,
243                                      unsigned int *events)
244 {
245         struct fio_rbd_iou *fri = io_u->engine_data;
246
247         if (fri->io_complete) {
248                 fri->io_seen = 1;
249                 rbd->aio_events[*events] = io_u;
250                 (*events)++;
251
252                 rbd_aio_release(fri->completion);
253                 return 1;
254         }
255
256         return 0;
257 }
258
259 static inline int rbd_io_u_seen(struct io_u *io_u)
260 {
261         struct fio_rbd_iou *fri = io_u->engine_data;
262
263         return fri->io_seen;
264 }
265
266 static void rbd_io_u_wait_complete(struct io_u *io_u)
267 {
268         struct fio_rbd_iou *fri = io_u->engine_data;
269
270         rbd_aio_wait_for_complete(fri->completion);
271 }
272
273 static int rbd_io_u_cmp(const void *p1, const void *p2)
274 {
275         const struct io_u **a = (const struct io_u **) p1;
276         const struct io_u **b = (const struct io_u **) p2;
277         uint64_t at, bt;
278
279         at = utime_since_now(&(*a)->start_time);
280         bt = utime_since_now(&(*b)->start_time);
281
282         if (at < bt)
283                 return -1;
284         else if (at == bt)
285                 return 0;
286         else
287                 return 1;
288 }
289
290 static int rbd_iter_events(struct thread_data *td, unsigned int *events,
291                            unsigned int min_evts, int wait)
292 {
293         struct rbd_data *rbd = td->io_ops_data;
294         unsigned int this_events = 0;
295         struct io_u *io_u;
296         int i, sidx;
297
298         sidx = 0;
299         io_u_qiter(&td->io_u_all, io_u, i) {
300                 if (!(io_u->flags & IO_U_F_FLIGHT))
301                         continue;
302                 if (rbd_io_u_seen(io_u))
303                         continue;
304
305                 if (fri_check_complete(rbd, io_u, events))
306                         this_events++;
307                 else if (wait)
308                         rbd->sort_events[sidx++] = io_u;
309         }
310
311         if (!wait || !sidx)
312                 return this_events;
313
314         /*
315          * Sort events, oldest issue first, then wait on as many as we
316          * need in order of age. If we have enough events, stop waiting,
317          * and just check if any of the older ones are done.
318          */
319         if (sidx > 1)
320                 qsort(rbd->sort_events, sidx, sizeof(struct io_u *), rbd_io_u_cmp);
321
322         for (i = 0; i < sidx; i++) {
323                 io_u = rbd->sort_events[i];
324
325                 if (fri_check_complete(rbd, io_u, events)) {
326                         this_events++;
327                         continue;
328                 }
329
330                 /*
331                  * Stop waiting when we have enough, but continue checking
332                  * all pending IOs if they are complete.
333                  */
334                 if (*events >= min_evts)
335                         continue;
336
337                 rbd_io_u_wait_complete(io_u);
338
339                 if (fri_check_complete(rbd, io_u, events))
340                         this_events++;
341         }
342
343         return this_events;
344 }
345
346 static int fio_rbd_getevents(struct thread_data *td, unsigned int min,
347                              unsigned int max, const struct timespec *t)
348 {
349         unsigned int this_events, events = 0;
350         struct rbd_options *o = td->eo;
351         int wait = 0;
352
353         do {
354                 this_events = rbd_iter_events(td, &events, min, wait);
355
356                 if (events >= min)
357                         break;
358                 if (this_events)
359                         continue;
360
361                 if (!o->busy_poll)
362                         wait = 1;
363                 else
364                         nop;
365         } while (1);
366
367         return events;
368 }
369
370 static int fio_rbd_queue(struct thread_data *td, struct io_u *io_u)
371 {
372         struct rbd_data *rbd = td->io_ops_data;
373         struct fio_rbd_iou *fri = io_u->engine_data;
374         int r = -1;
375
376         fio_ro_check(td, io_u);
377
378         fri->io_seen = 0;
379         fri->io_complete = 0;
380
381         r = rbd_aio_create_completion(fri, _fio_rbd_finish_aiocb,
382                                                 &fri->completion);
383         if (r < 0) {
384                 log_err("rbd_aio_create_completion failed.\n");
385                 goto failed;
386         }
387
388         if (io_u->ddir == DDIR_WRITE) {
389                 r = rbd_aio_write(rbd->image, io_u->offset, io_u->xfer_buflen,
390                                          io_u->xfer_buf, fri->completion);
391                 if (r < 0) {
392                         log_err("rbd_aio_write failed.\n");
393                         goto failed_comp;
394                 }
395
396         } else if (io_u->ddir == DDIR_READ) {
397                 r = rbd_aio_read(rbd->image, io_u->offset, io_u->xfer_buflen,
398                                         io_u->xfer_buf, fri->completion);
399
400                 if (r < 0) {
401                         log_err("rbd_aio_read failed.\n");
402                         goto failed_comp;
403                 }
404         } else if (io_u->ddir == DDIR_TRIM) {
405                 r = rbd_aio_discard(rbd->image, io_u->offset,
406                                         io_u->xfer_buflen, fri->completion);
407                 if (r < 0) {
408                         log_err("rbd_aio_discard failed.\n");
409                         goto failed_comp;
410                 }
411         } else if (io_u->ddir == DDIR_SYNC) {
412                 r = rbd_aio_flush(rbd->image, fri->completion);
413                 if (r < 0) {
414                         log_err("rbd_flush failed.\n");
415                         goto failed_comp;
416                 }
417         } else {
418                 dprint(FD_IO, "%s: Warning: unhandled ddir: %d\n", __func__,
419                        io_u->ddir);
420                 goto failed_comp;
421         }
422
423         return FIO_Q_QUEUED;
424 failed_comp:
425         rbd_aio_release(fri->completion);
426 failed:
427         io_u->error = r;
428         td_verror(td, io_u->error, "xfer");
429         return FIO_Q_COMPLETED;
430 }
431
432 static int fio_rbd_init(struct thread_data *td)
433 {
434         int r;
435
436         r = _fio_rbd_connect(td);
437         if (r) {
438                 log_err("fio_rbd_connect failed, return code: %d .\n", r);
439                 goto failed;
440         }
441
442         return 0;
443
444 failed:
445         return 1;
446 }
447
448 static void fio_rbd_cleanup(struct thread_data *td)
449 {
450         struct rbd_data *rbd = td->io_ops_data;
451
452         if (rbd) {
453                 _fio_rbd_disconnect(rbd);
454                 free(rbd->aio_events);
455                 free(rbd->sort_events);
456                 free(rbd);
457         }
458 }
459
460 static int fio_rbd_setup(struct thread_data *td)
461 {
462         rbd_image_info_t info;
463         struct fio_file *f;
464         struct rbd_data *rbd = NULL;
465         int major, minor, extra;
466         int r;
467
468         /* log version of librbd. No cluster connection required. */
469         rbd_version(&major, &minor, &extra);
470         log_info("rbd engine: RBD version: %d.%d.%d\n", major, minor, extra);
471
472         /* allocate engine specific structure to deal with librbd. */
473         r = _fio_setup_rbd_data(td, &rbd);
474         if (r) {
475                 log_err("fio_setup_rbd_data failed.\n");
476                 goto cleanup;
477         }
478         td->io_ops_data = rbd;
479
480         /* librbd does not allow us to run first in the main thread and later
481          * in a fork child. It needs to be the same process context all the
482          * time. 
483          */
484         td->o.use_thread = 1;
485
486         /* connect in the main thread to determine to determine
487          * the size of the given RADOS block device. And disconnect
488          * later on.
489          */
490         r = _fio_rbd_connect(td);
491         if (r) {
492                 log_err("fio_rbd_connect failed.\n");
493                 goto cleanup;
494         }
495
496         /* get size of the RADOS block device */
497         r = rbd_stat(rbd->image, &info, sizeof(info));
498         if (r < 0) {
499                 log_err("rbd_status failed.\n");
500                 goto disconnect;
501         }
502         dprint(FD_IO, "rbd-engine: image size: %lu\n", info.size);
503
504         /* taken from "net" engine. Pretend we deal with files,
505          * even if we do not have any ideas about files.
506          * The size of the RBD is set instead of a artificial file.
507          */
508         if (!td->files_index) {
509                 add_file(td, td->o.filename ? : "rbd", 0, 0);
510                 td->o.nr_files = td->o.nr_files ? : 1;
511                 td->o.open_files++;
512         }
513         f = td->files[0];
514         f->real_file_size = info.size;
515
516         /* disconnect, then we were only connected to determine
517          * the size of the RBD.
518          */
519         _fio_rbd_disconnect(rbd);
520         return 0;
521
522 disconnect:
523         _fio_rbd_disconnect(rbd);
524 cleanup:
525         fio_rbd_cleanup(td);
526         return r;
527 }
528
529 static int fio_rbd_open(struct thread_data *td, struct fio_file *f)
530 {
531         return 0;
532 }
533
534 static int fio_rbd_invalidate(struct thread_data *td, struct fio_file *f)
535 {
536 #if defined(CONFIG_RBD_INVAL)
537         struct rbd_data *rbd = td->io_ops_data;
538
539         return rbd_invalidate_cache(rbd->image);
540 #else
541         return 0;
542 #endif
543 }
544
545 static void fio_rbd_io_u_free(struct thread_data *td, struct io_u *io_u)
546 {
547         struct fio_rbd_iou *fri = io_u->engine_data;
548
549         if (fri) {
550                 io_u->engine_data = NULL;
551                 free(fri);
552         }
553 }
554
555 static int fio_rbd_io_u_init(struct thread_data *td, struct io_u *io_u)
556 {
557         struct fio_rbd_iou *fri;
558
559         fri = calloc(1, sizeof(*fri));
560         fri->io_u = io_u;
561         io_u->engine_data = fri;
562         return 0;
563 }
564
565 static struct ioengine_ops ioengine = {
566         .name                   = "rbd",
567         .version                = FIO_IOOPS_VERSION,
568         .setup                  = fio_rbd_setup,
569         .init                   = fio_rbd_init,
570         .queue                  = fio_rbd_queue,
571         .getevents              = fio_rbd_getevents,
572         .event                  = fio_rbd_event,
573         .cleanup                = fio_rbd_cleanup,
574         .open_file              = fio_rbd_open,
575         .invalidate             = fio_rbd_invalidate,
576         .options                = options,
577         .io_u_init              = fio_rbd_io_u_init,
578         .io_u_free              = fio_rbd_io_u_free,
579         .option_struct_size     = sizeof(struct rbd_options),
580 };
581
582 static void fio_init fio_rbd_register(void)
583 {
584         register_ioengine(&ioengine);
585 }
586
587 static void fio_exit fio_rbd_unregister(void)
588 {
589         unregister_ioengine(&ioengine);
590 }