Debian Codegraph @

xlators/storage/posix/src/posix-aio.c

raw symbols SCIP
1 /*
2 Copyright (c) 2006-2012 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 #include "posix.h"
11 #include <sys/uio.h>
12 #include "posix-messages.h"
13
14 #ifdef HAVE_LIBAIO
15 #include <libaio.h>
16
17 void
18 __posix_fd_set_odirect(fd_t *fd, struct posix_fd *pfd, int opflags, int direct)
19 {
20 int odirect = 0;
21 int flags = 0;
22 int ret = 0;
23
24 odirect = pfd->odirect;
25
26 if ((fd->flags | opflags) & O_DIRECT) {
27 /* Always use O_DIRECT if requested. */
28 odirect = 1;
29 } else
30 /* Use hint from the caller. */
31 odirect = direct;
32
33 if (!odirect && pfd->odirect) {
34 flags = fcntl(pfd->fd, F_GETFL);
35 ret = fcntl(pfd->fd, F_SETFL, (flags & (~O_DIRECT)));
36 pfd->odirect = 0;
37 }
38
39 if (odirect && !pfd->odirect) {
40 flags = fcntl(pfd->fd, F_GETFL);
41 ret = fcntl(pfd->fd, F_SETFL, (flags | O_DIRECT));
42 pfd->odirect = 1;
43 }
44
45 if (ret) {
46 gf_msg(THIS->name, GF_LOG_WARNING, errno, P_MSG_FCNTL_FAILED,
47 "fcntl() failed. fd=%d flags=%d pfd->odirect=%d", pfd->fd, flags,
48 pfd->odirect);
49 }
50 }
51
52 struct posix_aio_cb {
53 struct iocb iocb;
54 call_frame_t *frame;
55 struct iobuf *iobuf;
56 struct iobref *iobref;
57 struct iatt prebuf;
58 int _fd;
59 int op;
60 fd_t *fd;
61 off_t offset;
62 };
63
64 static struct posix_aio_cb *
65 posix_aio_cb_init(call_frame_t *frame, fd_t *fd, int _fd, int op)
66 {
67 struct posix_aio_cb *paiocb;
68
69 paiocb = GF_CALLOC(1, sizeof(*paiocb), gf_posix_mt_paiocb);
70 if (!paiocb)
71 return NULL;
72
73 paiocb->frame = frame;
74 paiocb->fd = fd_ref(fd);
75 paiocb->_fd = _fd;
76 paiocb->op = op;
77
78 paiocb->iocb.data = paiocb;
79 paiocb->iocb.aio_fildes = _fd;
80 paiocb->iocb.aio_reqprio = 0;
81
82 return paiocb;
83 }
84
85 static void
86 posix_aio_cb_fini(struct posix_aio_cb *paiocb)
87 {
88 if (paiocb) {
89 if (paiocb->iobuf)
90 iobuf_unref(paiocb->iobuf);
91 if (paiocb->iobref)
92 iobref_unref(paiocb->iobref);
93 if (paiocb->fd)
94 fd_unref(paiocb->fd);
95 GF_FREE(paiocb);
96 }
97 }
98
99 static int
100 posix_aio_readv_complete(struct posix_aio_cb *paiocb, int res)
101 {
102 call_frame_t *frame = NULL;
103 xlator_t *this = NULL;
104 struct iobuf *iobuf = NULL;
105 struct iatt postbuf = {
106 0,
107 };
108 int _fd = -1;
109 int op_ret = -1;
110 int op_errno = 0;
111 struct iovec iov;
112 int ret = 0;
113 off_t offset = 0;
114 struct posix_private *priv = NULL;
115 fd_t *fd = NULL;
116
117 GF_ASSERT(paiocb);
118
119 frame = paiocb->frame;
120 this = frame->this;
121 priv = this->private;
122 iobuf = paiocb->iobuf;
123 fd = paiocb->fd;
124 _fd = paiocb->_fd;
125 offset = paiocb->offset;
126
127 if (res < 0) {
128 op_ret = -1;
129 op_errno = -res;
130 gf_msg(this->name, GF_LOG_ERROR, op_errno, P_MSG_READV_FAILED,
131 "readv(async) failed fd=%d,size=%lu,offset=%llu (%d)", _fd,
132 paiocb->iocb.u.c.nbytes, (unsigned long long)paiocb->offset,
133 res);
134 goto out;
135 }
136
137 ret = posix_fdstat(this, fd->inode, _fd, &postbuf, _gf_true);
138 if (ret != 0) {
139 op_ret = -1;
140 op_errno = errno;
141 gf_msg(this->name, GF_LOG_ERROR, op_errno, P_MSG_FSTAT_FAILED,
142 "fstat failed on fd=%d", _fd);
143 goto out;
144 }
145
146 op_ret = res;
147 op_errno = 0;
148
149 paiocb->iobref = iobref_new();
150 if (!paiocb->iobref) {
151 op_ret = -1;
152 op_errno = ENOMEM;
153 goto out;
154 }
155
156 iobref_add(paiocb->iobref, iobuf);
157
158 iov.iov_base = iobuf_ptr(iobuf);
159 iov.iov_len = op_ret;
160
161 /* Hack to notify higher layers of EOF. */
162 if (!postbuf.ia_size || (offset + iov.iov_len) >= postbuf.ia_size)
163 op_errno = ENOENT;
164
165 GF_ATOMIC_ADD(priv->read_value, op_ret);
166
167 out:
168 STACK_UNWIND_STRICT(readv, frame, op_ret, op_errno, &iov, 1, &postbuf,
169 paiocb->iobref, NULL);
170
171 posix_aio_cb_fini(paiocb);
172
173 return 0;
174 }
175
176 static int
177 posix_aio_readv(call_frame_t *frame, xlator_t *this, fd_t *fd, size_t size,
178 off_t offset, uint32_t flags, dict_t *xdata)
179 {
180 int32_t op_errno = EINVAL;
181 int _fd = -1;
182 struct posix_fd *pfd = NULL;
183 int ret = -1;
184 struct posix_aio_cb *paiocb = NULL;
185 struct posix_private *priv = NULL;
186 struct iocb *iocb = NULL;
187
188 VALIDATE_OR_GOTO(frame, err);
189 VALIDATE_OR_GOTO(this, err);
190 VALIDATE_OR_GOTO(fd, err);
191
192 priv = this->private;
193
194 ret = posix_fd_ctx_get(fd, this, &pfd, &op_errno);
195 if (ret < 0) {
196 gf_msg(this->name, GF_LOG_WARNING, op_errno, P_MSG_PFD_NULL,
197 "pfd is NULL from fd=%p", fd);
198 goto err;
199 }
200 _fd = pfd->fd;
201
202 if (!size) {
203 op_errno = EINVAL;
204 gf_msg(this->name, GF_LOG_WARNING, op_errno, P_MSG_INVALID_ARGUMENT,
205 "size=%" GF_PRI_SIZET, size);
206 goto err;
207 }
208
209 paiocb = posix_aio_cb_init(frame, fd, _fd, GF_FOP_READ);
210 if (!paiocb) {
211 op_errno = ENOMEM;
212 goto err;
213 }
214
215 paiocb->iobuf = iobuf_get2(this->ctx->iobuf_pool, size);
216 if (!paiocb->iobuf) {
217 op_errno = ENOMEM;
218 goto err;
219 }
220
221 paiocb->offset = offset;
222
223 paiocb->iocb.aio_lio_opcode = IO_CMD_PREAD;
224 paiocb->iocb.u.c.buf = iobuf_ptr(paiocb->iobuf);
225 paiocb->iocb.u.c.nbytes = size;
226 paiocb->iocb.u.c.offset = offset;
227
228 iocb = &paiocb->iocb;
229
230 LOCK(&fd->lock);
231 {
232 __posix_fd_set_odirect(
233 fd, pfd, flags,
234 (DIRECT_ALIGNED(size, priv) && DIRECT_ALIGNED(offset, priv) &&
235 DIRECT_ALIGNED(iobuf_ptr(paiocb->iobuf), priv)));
236
237 ret = io_submit(priv->ctxp, 1, &iocb);
238 }
239 UNLOCK(&fd->lock);
240
241 if (ret != 1) {
242 op_errno = -ret;
243 gf_msg(this->name, GF_LOG_ERROR, op_errno, P_MSG_IO_SUBMIT_FAILED,
244 "io_submit() returned %d", ret);
245 goto err;
246 }
247
248 return 0;
249 err:
250 STACK_UNWIND_STRICT(readv, frame, -1, op_errno, 0, 0, 0, 0, 0);
251
252 posix_aio_cb_fini(paiocb);
253
254 return 0;
255 }
256
257 static int
258 posix_aio_writev_complete(struct posix_aio_cb *paiocb, int res)
259 {
260 call_frame_t *frame = NULL;
261 xlator_t *this = NULL;
262 struct iatt prebuf = {
263 0,
264 };
265 struct iatt postbuf = {
266 0,
267 };
268 int _fd = -1;
269 int op_ret = -1;
270 int op_errno = 0;
271 int ret = 0;
272 struct posix_private *priv = NULL;
273 fd_t *fd = NULL;
274
275 GF_ASSERT(paiocb);
276
277 frame = paiocb->frame;
278 this = frame->this;
279 priv = this->private;
280 prebuf = paiocb->prebuf;
281 fd = paiocb->fd;
282 _fd = paiocb->_fd;
283
284 if (res < 0) {
285 op_ret = -1;
286 op_errno = -res;
287 gf_msg(this->name, GF_LOG_ERROR, op_errno, P_MSG_WRITEV_FAILED,
288 "writev(async) failed fd=%d,offset=%llu (%d)", _fd,
289 (unsigned long long)paiocb->offset, res);
290
291 goto out;
292 }
293
294 ret = posix_fdstat(this, fd->inode, _fd, &postbuf, _gf_true);
295 if (ret != 0) {
296 op_ret = -1;
297 op_errno = errno;
298 gf_msg(this->name, GF_LOG_ERROR, op_errno, P_MSG_FSTAT_FAILED,
299 "fstat failed on fd=%d", _fd);
300 goto out;
301 }
302
303 op_ret = res;
304 op_errno = 0;
305
306 GF_ATOMIC_ADD(priv->write_value, op_ret);
307
308 out:
309 STACK_UNWIND_STRICT(writev, frame, op_ret, op_errno, &prebuf, &postbuf,
310 NULL);
311
312 posix_aio_cb_fini(paiocb);
313
314 return 0;
315 }
316
317 static int
318 posix_aio_writev(call_frame_t *frame, xlator_t *this, fd_t *fd,
319 struct iovec *iov, int count, off_t offset, uint32_t flags,
320 struct iobref *iobref, dict_t *xdata)
321 {
322 int32_t op_errno = EINVAL;
323 int _fd = -1, i = 0;
324 struct posix_fd *pfd = NULL;
325 int ret = -1, direct = 0;
326 struct posix_aio_cb *paiocb = NULL;
327 struct posix_private *priv = NULL;
328 struct iocb *iocb = NULL;
329
330 VALIDATE_OR_GOTO(frame, err);
331 VALIDATE_OR_GOTO(this, err);
332 VALIDATE_OR_GOTO(fd, err);
333
334 priv = this->private;
335 DISK_SPACE_CHECK_AND_GOTO(frame, priv, xdata, op_errno, op_errno, err);
336
337 ret = posix_fd_ctx_get(fd, this, &pfd, &op_errno);
338 if (ret < 0) {
339 gf_msg(this->name, GF_LOG_WARNING, op_errno, P_MSG_PFD_NULL,
340 "pfd is NULL from fd=%p", fd);
341 goto err;
342 }
343 _fd = pfd->fd;
344
345 paiocb = posix_aio_cb_init(frame, fd, _fd, GF_FOP_WRITE);
346 if (!paiocb) {
347 op_errno = ENOMEM;
348 goto err;
349 }
350
351 paiocb->offset = offset;
352
353 paiocb->iobref = iobref_ref(iobref);
354 paiocb->iocb.aio_lio_opcode = IO_CMD_PWRITEV;
355 /* There's an iocb.u.v structure that could seem more appropriate for
356 * passing an iovec. However this structure is only compatible with what
357 * the kernel expects for little-endian architectures. So, we just pass
358 * the iovec using the generic structure to avoid problems. */
359 paiocb->iocb.u.c.buf = iov;
360 paiocb->iocb.u.c.nbytes = count;
361 paiocb->iocb.u.c.offset = offset;
362
363 iocb = &paiocb->iocb;
364
365 ret = posix_fdstat(this, fd->inode, _fd, &paiocb->prebuf, _gf_true);
366 if (ret != 0) {
367 op_errno = errno;
368 gf_msg(this->name, GF_LOG_ERROR, op_errno, P_MSG_FSTAT_FAILED,
369 "fstat failed on fd=%p", fd);
370 goto err;
371 }
372
373 direct = DIRECT_ALIGNED(offset, priv);
374 for (i = 0; direct && i < count; i++)
375 direct = (direct && DIRECT_ALIGNED(iov[i].iov_base, priv) &&
376 DIRECT_ALIGNED(iov[i].iov_len, priv));
377
378 LOCK(&fd->lock);
379 {
380 __posix_fd_set_odirect(fd, pfd, flags, direct);
381
382 ret = io_submit(priv->ctxp, 1, &iocb);
383 }
384 UNLOCK(&fd->lock);
385
386 if (ret != 1) {
387 op_errno = -ret;
388 gf_msg(this->name, GF_LOG_ERROR, op_errno, P_MSG_IO_SUBMIT_FAILED,
389 "io_submit() returned %d,gfid=%s", ret,
390 uuid_utoa(fd->inode->gfid));
391 goto err;
392 }
393
394 return 0;
395 err:
396 STACK_UNWIND_STRICT(writev, frame, -1, op_errno, 0, 0, 0);
397
398 posix_aio_cb_fini(paiocb);
399
400 return 0;
401 }
402
403 static int
404 posix_aio_fsync_complete(struct posix_aio_cb *paiocb, int res)
405 {
406 call_frame_t *frame = NULL;
407 xlator_t *this = NULL;
408 struct iatt prebuf = {
409 0,
410 };
411 struct iatt postbuf = {
412 0,
413 };
414 int _fd = -1;
415 int op_ret = -1;
416 int op_errno = 0;
417 int ret = 0;
418 fd_t *fd = NULL;
419
420 GF_ASSERT(paiocb);
421
422 frame = paiocb->frame;
423 this = frame->this;
424 prebuf = paiocb->prebuf;
425 fd = paiocb->fd;
426 _fd = paiocb->_fd;
427
428 if (res < 0) {
429 op_ret = -1;
430 op_errno = -res;
431 gf_msg(this->name, GF_LOG_ERROR, op_errno, P_MSG_FSYNC_FAILED,
432 "%s(async) failed fd=%d (%d)",
433 (paiocb->iocb.aio_lio_opcode == IO_CMD_FDSYNC ? "fdatasync"
434 : "fsync"),
435 _fd, res);
436 goto out;
437 }
438
439 ret = posix_fdstat(this, fd->inode, _fd, &postbuf, _gf_true);
440 if (ret != 0) {
441 op_ret = -1;
442 op_errno = errno;
443 gf_msg(this->name, GF_LOG_ERROR, op_errno, P_MSG_FSTAT_FAILED,
444 "fstat failed on fd=%d", _fd);
445 goto out;
446 }
447
448 op_ret = res;
449 op_errno = 0;
450
451 out:
452 STACK_UNWIND_STRICT(fsync, frame, op_ret, op_errno, &prebuf, &postbuf,
453 NULL);
454
455 posix_aio_cb_fini(paiocb);
456
457 return 0;
458 }
459
460 static int
461 posix_aio_fsync(call_frame_t *frame, xlator_t *this, fd_t *fd, int32_t datasync,
462 dict_t *xdata)
463 {
464 int32_t op_errno = EINVAL;
465 int _fd = -1, ret = -1;
466 struct posix_fd *pfd = NULL;
467 struct posix_aio_cb *paiocb = NULL;
468 struct posix_private *priv = NULL;
469 struct iocb *iocb = NULL;
470
471 VALIDATE_OR_GOTO(frame, err);
472 VALIDATE_OR_GOTO(this, err);
473 VALIDATE_OR_GOTO(fd, err);
474
475 priv = this->private;
476
477 ret = posix_fd_ctx_get(fd, this, &pfd, &op_errno);
478 if (ret < 0) {
479 gf_msg(this->name, GF_LOG_WARNING, op_errno, P_MSG_PFD_NULL,
480 "pfd is NULL from fd=%p", fd);
481 goto err;
482 }
483
484 _fd = pfd->fd;
485
486 paiocb = posix_aio_cb_init(frame, fd, _fd, GF_FOP_FSYNC);
487 if (!paiocb) {
488 op_errno = ENOMEM;
489 goto err;
490 }
491
492 paiocb->iocb.aio_lio_opcode = datasync ? IO_CMD_FDSYNC : IO_CMD_FSYNC;
493
494 iocb = &paiocb->iocb;
495
496 ret = posix_fdstat(this, fd->inode, _fd, &paiocb->prebuf, _gf_false);
497 if (ret != 0) {
498 op_errno = errno;
499 gf_msg(this->name, GF_LOG_ERROR, op_errno, P_MSG_FSTAT_FAILED,
500 "fstat failed on fd=%p", fd);
501 goto err;
502 }
503
504 ret = io_submit(priv->ctxp, 1, &iocb);
505
506 if (ret != 1) {
507 op_errno = -ret;
508 gf_msg(this->name, GF_LOG_ERROR, op_errno, P_MSG_IO_SUBMIT_FAILED,
509 "io_submit() returned %d,gfid=%s", ret,
510 uuid_utoa(fd->inode->gfid));
511 goto err;
512 }
513
514 return 0;
515
516 err:
517 STACK_UNWIND_STRICT(fsync, frame, -1, op_errno, 0, 0, 0);
518
519 posix_aio_cb_fini(paiocb);
520
521 return 0;
522 }
523
524 static void *
525 posix_aio_thread(void *data)
526 {
527 xlator_t *this = NULL;
528 struct posix_private *priv = NULL;
529 int ret = 0;
530 int i = 0;
531 struct io_event events[POSIX_AIO_MAX_NR_GETEVENTS];
532 struct io_event *event = NULL;
533 struct posix_aio_cb *paiocb = NULL;
534
535 this = data;
536 THIS = this;
537 priv = this->private;
538
539 for (;;) {
540 memset(&events[0], 0, sizeof(events));
541 ret = io_getevents(priv->ctxp, 1, POSIX_AIO_MAX_NR_GETEVENTS,
542 &events[0], NULL);
543 if (ret <= 0) {
544 gf_msg(this->name, GF_LOG_ERROR, -ret, P_MSG_IO_GETEVENTS_FAILED,
545 "io_getevents() returned %d", ret);
546 if (ret == -EINTR)
547 continue;
548 break;
549 }
550
551 for (i = 0; i < ret; i++) {
552 event = &events[i];
553
554 paiocb = event->data;
555
556 switch (paiocb->op) {
557 case GF_FOP_READ:
558 posix_aio_readv_complete(paiocb, event->res);
559 break;
560 case GF_FOP_WRITE:
561 posix_aio_writev_complete(paiocb, event->res);
562 break;
563 case GF_FOP_FSYNC:
564 posix_aio_fsync_complete(paiocb, event->res);
565 break;
566 default:
567 gf_msg(this->name, GF_LOG_ERROR, 0, P_MSG_UNKNOWN_OP,
568 "unknown op %d found in piocb", paiocb->op);
569 break;
570 }
571 }
572 }
573
574 return NULL;
575 }
576
577 static int
578 posix_aio_init(xlator_t *this)
579 {
580 struct posix_private *priv = NULL;
581 int ret = 0;
582
583 priv = this->private;
584
585 ret = io_setup(POSIX_AIO_MAX_NR_EVENTS, &priv->ctxp);
586 if ((ret == -1 && errno == ENOSYS) || ret == -ENOSYS) {
587 gf_msg(this->name, GF_LOG_WARNING, 0, P_MSG_AIO_UNAVAILABLE,
588 "Linux AIO not available at run-time."
589 " Continuing with synchronous IO");
590 ret = 0;
591 goto out;
592 }
593
594 if (ret < 0) {
595 gf_msg(this->name, GF_LOG_WARNING, -ret, P_MSG_IO_SETUP_FAILED,
596 "io_setup() failed. ret=%d", ret);
597 goto out;
598 }
599
600 ret = gf_thread_create(&priv->aiothread, NULL, posix_aio_thread, this,
601 "posixaio");
602 if (ret != 0) {
603 io_destroy(priv->ctxp);
604 goto out;
605 }
606 out:
607 return ret;
608 }
609
610 int
611 posix_aio_on(xlator_t *this)
612 {
613 struct posix_private *priv = NULL;
614 int ret = 0;
615
616 priv = this->private;
617
618 if (!priv->aio_init_done) {
619 ret = posix_aio_init(this);
620 if (ret == 0)
621 priv->aio_capable = _gf_true;
622 else
623 priv->aio_capable = _gf_false;
624 priv->aio_init_done = _gf_true;
625 }
626
627 if (priv->aio_capable) {
628 this->fops->readv = posix_aio_readv;
629 this->fops->writev = posix_aio_writev;
630 this->fops->fsync = posix_aio_fsync;
631 }
632
633 return ret;
634 }
635
636 int
637 posix_aio_off(xlator_t *this)
638 {
639 this->fops->readv = posix_readv;
640 this->fops->writev = posix_writev;
641 this->fops->fsync = posix_fsync;
642
643 return 0;
644 }
645
646 #else /* no HAVE_LIBAIO */
647
648 int
649 posix_aio_on(xlator_t *this)
650 {
651 gf_msg(this->name, GF_LOG_INFO, 0, P_MSG_AIO_UNAVAILABLE,
652 "Linux AIO not available at build-time."
653 " Continuing with synchronous IO");
654 return 0;
655 }
656
657 int
658 posix_aio_off(xlator_t *this)
659 {
660 gf_msg(this->name, GF_LOG_INFO, 0, P_MSG_AIO_UNAVAILABLE,
661 "Linux AIO not available at build-time."
662 " Continuing with synchronous IO");
663 return 0;
664 }
665
666 void
667 __posix_fd_set_odirect(fd_t *fd, struct posix_fd *pfd, int opflags, int direct)
668 {
669 xlator_t *this = THIS;
670 gf_msg(this->name, GF_LOG_INFO, 0, P_MSG_AIO_UNAVAILABLE,
671 "Linux AIO not available at build-time."
672 " Continuing with synchronous IO");
673 return;
674 }
675
676 #endif /* HAVE_LIBAIO */