Add more context to the error messages
[fio.git] / engines / sync.c
CommitLineData
2866c82d
JA
1/*
2 * regular read/write sync io engine
3 *
4 */
5#include <stdio.h>
6#include <stdlib.h>
7#include <unistd.h>
8#include <errno.h>
9#include <assert.h>
5f350952
JA
10
11#include "../fio.h"
12#include "../os.h"
2866c82d 13
2866c82d
JA
14static int fio_syncio_prep(struct thread_data *td, struct io_u *io_u)
15{
53cdc686
JA
16 struct fio_file *f = io_u->file;
17
87dc1ab1
JA
18 if (io_u->ddir == DDIR_SYNC)
19 return 0;
02bcaa8c
JA
20 if (io_u->offset == f->last_completed_pos)
21 return 0;
87dc1ab1 22
53cdc686 23 if (lseek(f->fd, io_u->offset, SEEK_SET) == -1) {
e1161c32 24 td_verror(td, errno, "lseek");
2866c82d
JA
25 return 1;
26 }
27
28 return 0;
29}
30
31static int fio_syncio_queue(struct thread_data *td, struct io_u *io_u)
32{
53cdc686 33 struct fio_file *f = io_u->file;
cec6b55d 34 int ret;
2866c82d
JA
35
36 if (io_u->ddir == DDIR_READ)
cec6b55d 37 ret = read(f->fd, io_u->xfer_buf, io_u->xfer_buflen);
87dc1ab1 38 else if (io_u->ddir == DDIR_WRITE)
cec6b55d 39 ret = write(f->fd, io_u->xfer_buf, io_u->xfer_buflen);
87dc1ab1
JA
40 else
41 ret = fsync(f->fd);
2866c82d 42
cec6b55d 43 if (ret != (int) io_u->xfer_buflen) {
22819ec2 44 if (ret >= 0) {
cec6b55d
JA
45 io_u->resid = io_u->xfer_buflen - ret;
46 io_u->error = 0;
36167d82 47 return FIO_Q_COMPLETED;
2866c82d
JA
48 } else
49 io_u->error = errno;
50 }
51
36167d82 52 if (io_u->error)
e1161c32 53 td_verror(td, io_u->error, "xfer");
2866c82d 54
36167d82 55 return FIO_Q_COMPLETED;
2866c82d
JA
56}
57
5f350952 58static struct ioengine_ops ioengine = {
2866c82d
JA
59 .name = "sync",
60 .version = FIO_IOOPS_VERSION,
2866c82d
JA
61 .prep = fio_syncio_prep,
62 .queue = fio_syncio_queue,
2866c82d
JA
63 .flags = FIO_SYNCIO,
64};
5f350952
JA
65
66static void fio_init fio_syncio_register(void)
67{
68 register_ioengine(&ioengine);
69}
70
71static void fio_exit fio_syncio_unregister(void)
72{
73 unregister_ioengine(&ioengine);
74}