Debian Codegraph @

xlators/storage/posix/src/posix-io-uring.c

raw symbols SCIP
1 /*
2 Copyright (c) 2020 Red Hat, Inc. <http://www.redhat.com>
3 This file is part of GlusterFS.
4
5 This file is licensed to you under your choice of the GNU Lesser
6 General Public License, version 3 or any later version (LGPLv3 or
7 later), or the GNU General Public License, version 2 (GPLv2), in all
8 cases as published by the Free Software Foundation.
9 */
10
11 #include "posix.h"
12 #include "posix-messages.h"
13 #include "posix-io-uring.h"
14 #include "posix-handle.h"
15
16 #ifdef HAVE_LIBURING
17 #include <liburing.h>
18
19 struct posix_uring_ctx;
20 typedef void(fop_unwind_f)(struct posix_uring_ctx *, int32_t);
21 typedef void(fop_prep_f)(struct io_uring_sqe *sqe, struct posix_uring_ctx *);
22 static int
23 posix_io_uring_submit(xlator_t *this, struct posix_uring_ctx *ctx);
24
25 struct posix_uring_ctx {
26 call_frame_t *frame;
27 struct iatt prebuf;
28 dict_t *xdata;
29 fd_t *fd;
30 int _fd;
31 int op;
32
33 union {
34 struct {
35 struct iovec *iov;
36 int count;
37 off_t offset;
38 } write;
39
40 struct {
41 struct iobuf *iobuf;
42 struct iovec iovec;
43 off_t offset;
44 } read;
45
46 struct {
47 int32_t datasync;
48 } fsync;
49 } fop;
50
51 fop_prep_f *prepare;
52 fop_unwind_f *unwind;
53 };
54
55 static void
56 posix_io_uring_ctx_free(struct posix_uring_ctx *ctx)
57 {
58 if (!ctx)
59 return;
60 if (ctx->fd)
61 fd_unref(ctx->fd);
62 if (ctx->xdata)
63 dict_unref(ctx->xdata);
64 switch (ctx->op) {
65 case GF_FOP_READ:
66 if (ctx->fop.read.iobuf)
67 iobuf_unref(ctx->fop.read.iobuf);
68 break;
69 default:
70 break;
71 }
72 GF_FREE(ctx);
73 }
74
75 struct posix_uring_ctx *
76 posix_io_uring_ctx_init(call_frame_t *frame, xlator_t *this, fd_t *fd, int op,
77 fop_prep_f prepare, fop_unwind_f unwind,
78 int32_t *op_errno, dict_t *xdata)
79 {
80 struct posix_uring_ctx *ctx = NULL;
81 struct posix_fd *pfd = NULL;
82 int ret = 0;
83
84 ctx = GF_CALLOC(1, sizeof(*ctx), gf_posix_mt_uring_ctx);
85 if (!ctx) {
86 return NULL;
87 }
88
89 ctx->frame = frame;
90 ctx->fd = fd_ref(fd);
91 ctx->prepare = prepare;
92 ctx->unwind = unwind;
93 if (xdata)
94 ctx->xdata = dict_ref(xdata);
95 ctx->op = op;
96
97 ret = posix_fd_ctx_get(fd, this, &pfd, op_errno);
98 if (ret < 0) {
99 gf_msg(this->name, GF_LOG_WARNING, *op_errno, P_MSG_PFD_NULL,
100 "pfd is NULL from fd=%p", fd);
101 goto err;
102 }
103 ctx->_fd = pfd->fd;
104
105 /* TODO: Explore filling up pre and post bufs using IOSQE_IO_LINK*/
106 if ((op == GF_FOP_WRITE) || (op == GF_FOP_FSYNC)) {
107 if (posix_fdstat(this, fd->inode, pfd->fd, &ctx->prebuf, _gf_true) !=
108 0) {
109 *op_errno = errno;
110 gf_msg(this->name, GF_LOG_ERROR, *op_errno, P_MSG_FSTAT_FAILED,
111 "fstat failed on fd=%p", fd);
112 goto err;
113 }
114 }
115
116 return ctx;
117
118 err:
119 posix_io_uring_ctx_free(ctx);
120 return NULL;
121 }
122
123 static void
124 posix_io_uring_readv_complete(struct posix_uring_ctx *ctx, int32_t res)
125 {
126 call_frame_t *frame = NULL;
127 xlator_t *this = NULL;
128 struct posix_private *priv = NULL;
129 struct iobref *iobref = NULL;
130 struct iobuf *iobuf = NULL;
131 struct iatt postbuf = {
132 0,
133 };
134 struct iovec iov = {
135 0,
136 };
137 fd_t *fd = NULL;
138 int _fd = -1;
139 int ret = 0;
140 int op_ret = -1;
141 int op_errno = 0;
142 off_t offset = 0;
143
144 frame = ctx->frame;
145 this = frame->this;
146 priv = this->private;
147 fd = ctx->fd;
148 _fd = ctx->_fd;
149 iobuf = ctx->fop.read.iobuf;
150 offset = ctx->fop.read.offset;
151
152 if (res < 0) {
153 op_ret = -1;
154 op_errno = -res;
155 gf_msg(this->name, GF_LOG_ERROR, op_errno, P_MSG_READV_FAILED,
156 "readv(async) failed fd=%d.", _fd);
157 goto out;
158 }
159
160 ret = posix_fdstat(this, fd->inode, _fd, &postbuf, _gf_true);
161 if (ret != 0) {
162 op_ret = -1;
163 op_errno = errno;
164 gf_msg(this->name, GF_LOG_ERROR, op_errno, P_MSG_FSTAT_FAILED,
165 "fstat failed on fd=%d", _fd);
166 goto out;
167 }
168
169 op_ret = res;
170 op_errno = 0;
171
172 iobref = iobref_new();
173 if (!iobref) {
174 op_ret = -1;
175 op_errno = ENOMEM;
176 goto out;
177 }
178
179 iobref_add(iobref, iobuf);
180
181 iov.iov_base = iobuf_ptr(iobuf);
182 iov.iov_len = op_ret;
183
184 /* Hack to notify higher layers of EOF. */
185 if (!postbuf.ia_size || (offset + iov.iov_len) >= postbuf.ia_size)
186 op_errno = ENOENT;
187
188 GF_ATOMIC_ADD(priv->read_value, op_ret);
189 // response xdata is used only for cloudsync, so ignore for now.
190 out:
191 STACK_UNWIND_STRICT(readv, frame, op_ret, op_errno, &iov, 1, &postbuf,
192 iobref, NULL);
193 if (iobref)
194 iobref_unref(iobref);
195 posix_io_uring_ctx_free(ctx);
196 }
197
198 static void
199 posix_prep_readv(struct io_uring_sqe *sqe, struct posix_uring_ctx *ctx)
200 {
201 sqe->flags |= IOSQE_ASYNC;
202 io_uring_prep_readv(sqe, ctx->_fd, &ctx->fop.read.iovec, 1,
203 ctx->fop.read.offset);
204 }
205
206 static int
207 posix_io_uring_readv(call_frame_t *frame, xlator_t *this, fd_t *fd, size_t size,
208 off_t offset, uint32_t flags, dict_t *xdata)
209 {
210 struct posix_uring_ctx *ctx = NULL;
211 int32_t op_errno = ENOMEM;
212 struct iobuf *iobuf = NULL;
213 int ret = 0;
214
215 ctx = posix_io_uring_ctx_init(
216 frame, this, fd, GF_FOP_READ, posix_prep_readv,
217 posix_io_uring_readv_complete, &op_errno, xdata);
218 if (!ctx) {
219 goto err;
220 }
221
222 iobuf = iobuf_get2(this->ctx->iobuf_pool, size);
223 if (!iobuf) {
224 op_errno = ENOMEM;
225 goto err;
226 }
227 ctx->fop.read.iobuf = iobuf;
228 ctx->fop.read.iovec.iov_base = iobuf_ptr(iobuf);
229 ctx->fop.read.iovec.iov_len = size;
230 ctx->fop.read.offset = offset;
231
232 ret = posix_io_uring_submit(this, ctx);
233 if (ret < 0) {
234 gf_msg(this->name, GF_LOG_ERROR, -ret, P_MSG_POSIX_IO_URING,
235 "Failed to submit sqe");
236 op_errno = -ret;
237 goto err;
238 }
239 if (ret == 0) {
240 gf_msg(this->name, GF_LOG_WARNING, -ret, P_MSG_POSIX_IO_URING,
241 "submit sqe got zero");
242 }
243 return 0;
244 err:
245 STACK_UNWIND_STRICT(readv, frame, -1, op_errno, NULL, 1, NULL, NULL, NULL);
246 posix_io_uring_ctx_free(ctx);
247 return 0;
248 }
249
250 static void
251 posix_writev_fill_rsp_dict(struct posix_uring_ctx *ctx, xlator_t *this,
252 dict_t **rsp_xdata)
253 {
254 int is_append = 0;
255
256 if (ctx->xdata && dict_get(ctx->xdata, GLUSTERFS_WRITE_IS_APPEND)) {
257 if (ctx->prebuf.ia_size == ctx->fop.write.offset ||
258 (ctx->fd->flags & O_APPEND))
259 is_append = 1;
260 }
261 *rsp_xdata = _fill_writev_xdata(ctx->fd, ctx->xdata, this, is_append);
262 }
263
264 static void
265 posix_io_uring_writev_complete(struct posix_uring_ctx *ctx, int32_t res)
266 {
267 call_frame_t *frame = NULL;
268 xlator_t *this = NULL;
269 struct posix_private *priv = NULL;
270 struct iatt postbuf = {
271 0,
272 };
273 fd_t *fd = NULL;
274 int _fd = -1;
275 int ret = 0;
276 int op_ret = -1;
277 int op_errno = 0;
278 dict_t *rsp_xdata = NULL;
279 frame = ctx->frame;
280 this = frame->this;
281 priv = this->private;
282 fd = ctx->fd;
283 _fd = ctx->_fd;
284
285 if (res < 0) {
286 op_ret = -1;
287 op_errno = -res;
288 gf_msg(this->name, GF_LOG_ERROR, op_errno, P_MSG_WRITEV_FAILED,
289 "writev(async) failed fd=%d.", _fd);
290 goto out;
291 }
292
293 ret = posix_fdstat(this, fd->inode, _fd, &postbuf, _gf_true);
294 if (ret != 0) {
295 op_ret = -1;
296 op_errno = errno;
297 gf_msg(this->name, GF_LOG_ERROR, op_errno, P_MSG_FSTAT_FAILED,
298 "fstat failed on fd=%d", _fd);
299 goto out;
300 }
301
302 op_ret = res;
303 op_errno = 0;
304 posix_writev_fill_rsp_dict(ctx, this, &rsp_xdata);
305 GF_ATOMIC_ADD(priv->write_value, op_ret);
306 out:
307 STACK_UNWIND_STRICT(writev, frame, op_ret, op_errno, &ctx->prebuf, &postbuf,
308 rsp_xdata);
309 if (rsp_xdata)
310 dict_unref(rsp_xdata);
311 posix_io_uring_ctx_free(ctx);
312 }
313
314 static void
315 posix_prep_writev(struct io_uring_sqe *sqe, struct posix_uring_ctx *ctx)
316 {
317 io_uring_prep_writev(sqe, ctx->_fd, ctx->fop.write.iov,
318 ctx->fop.write.count, ctx->fop.write.offset);
319 }
320
321 static int
322 posix_io_uring_writev(call_frame_t *frame, xlator_t *this, fd_t *fd,
323 struct iovec *iov, int count, off_t offset,
324 uint32_t flags, struct iobref *iobref, dict_t *xdata)
325 {
326 struct posix_uring_ctx *ctx = NULL;
327 int32_t op_errno = ENOMEM;
328 int ret = 0;
329
330 ctx = posix_io_uring_ctx_init(
331 frame, this, fd, GF_FOP_WRITE, posix_prep_writev,
332 posix_io_uring_writev_complete, &op_errno, xdata);
333 if (!ctx) {
334 goto err;
335 }
336
337 ctx->fop.write.iov = iov;
338 ctx->fop.write.count = count;
339 ctx->fop.write.offset = offset;
340
341 ret = posix_io_uring_submit(this, ctx);
342 if (ret < 0) {
343 gf_msg(this->name, GF_LOG_ERROR, -ret, P_MSG_POSIX_IO_URING,
344 "Failed to submit sqe");
345 op_errno = -ret;
346 goto err;
347 }
348 if (ret == 0) {
349 gf_msg(this->name, GF_LOG_WARNING, -ret, P_MSG_POSIX_IO_URING,
350 "submit sqe got zero");
351 }
352 return 0;
353 err:
354 STACK_UNWIND_STRICT(writev, frame, -1, op_errno, 0, 0, 0);
355 posix_io_uring_ctx_free(ctx);
356 return 0;
357 }
358
359 static void
360 posix_io_uring_fsync_complete(struct posix_uring_ctx *ctx, int32_t res)
361 {
362 call_frame_t *frame = NULL;
363 xlator_t *this = NULL;
364 struct posix_private *priv = NULL;
365 struct iatt postbuf = {
366 0,
367 };
368 fd_t *fd = NULL;
369 int _fd = -1;
370 int ret = 0;
371 int op_ret = -1;
372 int op_errno = 0;
373
374 frame = ctx->frame;
375 this = frame->this;
376 priv = this->private;
377 fd = ctx->fd;
378 _fd = ctx->_fd;
379
380 if (res < 0) {
381 op_ret = -1;
382 op_errno = -res;
383 gf_msg(this->name, GF_LOG_ERROR, op_errno, P_MSG_FSYNC_FAILED,
384 "writev(async) failed fd=%d.", _fd);
385 goto out;
386 }
387
388 ret = posix_fdstat(this, fd->inode, _fd, &postbuf, _gf_true);
389 if (ret != 0) {
390 op_ret = -1;
391 op_errno = errno;
392 gf_msg(this->name, GF_LOG_ERROR, op_errno, P_MSG_FSTAT_FAILED,
393 "fstat failed on fd=%d", _fd);
394 goto out;
395 }
396
397 op_ret = res;
398 op_errno = 0;
399 GF_ATOMIC_ADD(priv->write_value, op_ret);
400 out:
401 STACK_UNWIND_STRICT(fsync, frame, op_ret, op_errno, &ctx->prebuf, &postbuf,
402 NULL);
403 posix_io_uring_ctx_free(ctx);
404 }
405
406 static void
407 posix_prep_fsync(struct io_uring_sqe *sqe, struct posix_uring_ctx *ctx)
408 {
409 io_uring_prep_fsync(sqe, ctx->_fd, ctx->fop.fsync.datasync);
410 }
411
412 static int
413 posix_io_uring_fsync(call_frame_t *frame, xlator_t *this, fd_t *fd,
414 int32_t datasync, dict_t *xdata)
415 {
416 struct posix_uring_ctx *ctx = NULL;
417 int32_t op_errno = ENOMEM;
418 int ret = 0;
419
420 ctx = posix_io_uring_ctx_init(
421 frame, this, fd, GF_FOP_FSYNC, posix_prep_fsync,
422 posix_io_uring_fsync_complete, &op_errno, xdata);
423 if (!ctx) {
424 goto err;
425 }
426
427 if (datasync)
428 ctx->fop.fsync.datasync |= IORING_FSYNC_DATASYNC;
429
430 ret = posix_io_uring_submit(this, ctx);
431 if (ret < 0) {
432 gf_msg(this->name, GF_LOG_ERROR, -ret, P_MSG_POSIX_IO_URING,
433 "Failed to submit sqe");
434 op_errno = -ret;
435 goto err;
436 }
437 if (ret == 0) {
438 gf_msg(this->name, GF_LOG_ERROR, -ret, P_MSG_POSIX_IO_URING,
439 "submit sqe got zero");
440 }
441 return 0;
442 err:
443 posix_io_uring_ctx_free(ctx);
444 STACK_UNWIND_STRICT(fsync, frame, -1, op_errno, 0, 0, NULL);
445 return 0;
446 }
447
448 static int
449 posix_io_uring_submit(xlator_t *this, struct posix_uring_ctx *ctx)
450 {
451 struct posix_private *priv = this->private;
452 struct io_uring_sqe *sqe = NULL;
453 int ret = 0;
454
455 pthread_mutex_lock(&priv->sq_mutex);
456 {
457 sqe = io_uring_get_sqe(&priv->ring);
458 if (!sqe) {
459 /*TODO: Retry until we get an sqe instead of failing. */
460 pthread_mutex_unlock(&priv->sq_mutex);
461 ret = -EAGAIN;
462 gf_msg(this->name, GF_LOG_ERROR, -ret, P_MSG_POSIX_IO_URING,
463 "Failed to get sqe");
464 goto out;
465 }
466 ctx->prepare(sqe, ctx);
467 io_uring_sqe_set_data(sqe, ctx);
468 ret = io_uring_submit(&priv->ring);
469 }
470 pthread_mutex_unlock(&priv->sq_mutex);
471
472 out:
473 return ret;
474 }
475
476 static void *
477 posix_io_uring_thread(void *data)
478 {
479 xlator_t *this = NULL;
480 struct posix_private *priv = NULL;
481 int ret = 0;
482 int32_t res = 0;
483 struct io_uring_cqe *cqe = NULL;
484 struct posix_uring_ctx *ctx = NULL;
485
486 this = data;
487 THIS = this;
488 priv = this->private;
489 while (1) {
490 pthread_mutex_lock(&priv->cq_mutex);
491 {
492 ret = io_uring_wait_cqe(&priv->ring, &cqe);
493 }
494 pthread_mutex_unlock(&priv->cq_mutex);
495 if (ret != 0) {
496 if (ret == -EINTR)
497 continue;
498 gf_msg(this->name, GF_LOG_WARNING, -ret, P_MSG_POSIX_IO_URING,
499 "Unable to get cqe. Exiting.");
500 abort();
501 }
502
503 ctx = (struct posix_uring_ctx *)io_uring_cqe_get_data(cqe);
504 if (priv->uring_thread_exit == _gf_true && ctx == NULL)
505 pthread_exit(NULL);
506 res = cqe->res;
507 io_uring_cqe_seen(&priv->ring, cqe);
508 ctx->unwind(ctx, res);
509 }
510
511 return NULL;
512 }
513
514 int
515 posix_io_uring_init(xlator_t *this)
516 {
517 int ret = -1;
518 unsigned flags = 0;
519 struct posix_private *priv = this->private;
520
521 // TODO:Try-out flags |= IORING_SETUP_IOPOLL;
522 ret = io_uring_queue_init(POSIX_URING_MAX_ENTRIES, &priv->ring, flags);
523 if (ret == -1) {
524 gf_msg(this->name, GF_LOG_ERROR, 0, P_MSG_POSIX_IO_URING,
525 "io_uring init failed.");
526 goto out;
527 }
528
529 pthread_mutex_init(&priv->sq_mutex, NULL);
530 pthread_mutex_init(&priv->cq_mutex, NULL);
531 ret = gf_thread_create(&priv->uring_thread, NULL, posix_io_uring_thread,
532 this, "posix-iouring");
533 if (ret != 0) {
534 io_uring_queue_exit(&priv->ring);
535 pthread_mutex_destroy(&priv->sq_mutex);
536 pthread_mutex_destroy(&priv->cq_mutex);
537 goto out;
538 }
539
540 out:
541 return ret;
542 }
543
544 static int
545 posix_io_uring_drain(struct posix_private *priv)
546 {
547 struct io_uring_sqe *sqe = NULL;
548 int ret = -1;
549
550 priv->uring_thread_exit = _gf_true;
551 sqe = io_uring_get_sqe(&priv->ring);
552 if (!sqe)
553 return ret;
554
555 io_uring_sqe_set_flags(sqe, IOSQE_IO_DRAIN);
556 io_uring_sqe_set_data(sqe, NULL);
557 io_uring_prep_nop(sqe);
558 ret = io_uring_submit(&priv->ring);
559
560 return ret;
561 }
562
563 static void
564 posix_io_uring_fini(struct posix_private *priv)
565 {
566 posix_io_uring_drain(priv);
567 (void)pthread_join(priv->uring_thread, NULL);
568 io_uring_queue_exit(&priv->ring);
569 pthread_mutex_destroy(&priv->sq_mutex);
570 pthread_mutex_destroy(&priv->cq_mutex);
571 }
572
573 int
574 posix_io_uring_on(xlator_t *this)
575 {
576 struct posix_private *priv = NULL;
577 int ret = -1;
578
579 priv = this->private;
580
581 if (!priv->io_uring_init_done) {
582 ret = posix_io_uring_init(this);
583 if (ret == 0)
584 priv->io_uring_capable = _gf_true;
585 else
586 priv->io_uring_capable = _gf_false;
587 priv->io_uring_init_done = _gf_true;
588 }
589
590 if (priv->io_uring_capable) {
591 this->fops->readv = posix_io_uring_readv;
592 this->fops->writev = posix_io_uring_writev;
593 this->fops->fsync = posix_io_uring_fsync;
594 ret = 0;
595 }
596
597 if (ret != 0) {
598 gf_msg(this->name, GF_LOG_WARNING, 0, P_MSG_POSIX_IO_URING,
599 "Posix io_uring init failed, falling back to the previous "
600 "IO mechanism.");
601 }
602 return ret;
603 }
604
605 int
606 posix_io_uring_off(xlator_t *this)
607 {
608 struct posix_private *priv = this->private;
609
610 this->fops->readv = posix_readv;
611 this->fops->writev = posix_writev;
612 this->fops->fsync = posix_fsync;
613 if (priv->io_uring_capable)
614 posix_io_uring_fini(priv);
615
616 return 0;
617 }
618
619 #else
620 int
621 posix_io_uring_on(xlator_t *this)
622 {
623 gf_msg(this->name, GF_LOG_WARNING, 0, P_MSG_AIO_UNAVAILABLE,
624 "Linux io_uring not available at build-time. "
625 "Continuing with synchronous IO");
626 return -1;
627 }
628
629 int
630 posix_io_uring_off(xlator_t *this)
631 {
632 return 0;
633 }
634
635 #endif