syslet engine: fix segfault if syslets are not available
[fio.git] / engines / sync.c
1 /*
2  * sync engine
3  *
4  * IO engine that does regular read(2)/write(2) with lseek(2) to transfer
5  * data.
6  *
7  */
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <errno.h>
12 #include <assert.h>
13
14 #include "../fio.h"
15 #include "../os.h"
16
17 static int fio_syncio_prep(struct thread_data *td, struct io_u *io_u)
18 {
19         struct fio_file *f = io_u->file;
20
21         if (io_u->ddir == DDIR_SYNC)
22                 return 0;
23         if (io_u->offset == f->last_completed_pos)
24                 return 0;
25
26         if (lseek(f->fd, io_u->offset, SEEK_SET) == -1) {
27                 td_verror(td, errno, "lseek");
28                 return 1;
29         }
30
31         return 0;
32 }
33
34 static int fio_syncio_queue(struct thread_data *td, struct io_u *io_u)
35 {
36         struct fio_file *f = io_u->file;
37         int ret;
38
39         if (io_u->ddir == DDIR_READ)
40                 ret = read(f->fd, io_u->xfer_buf, io_u->xfer_buflen);
41         else if (io_u->ddir == DDIR_WRITE)
42                 ret = write(f->fd, io_u->xfer_buf, io_u->xfer_buflen);
43         else
44                 ret = fsync(f->fd);
45
46         if (ret != (int) io_u->xfer_buflen) {
47                 if (ret >= 0) {
48                         io_u->resid = io_u->xfer_buflen - ret;
49                         io_u->error = 0;
50                         return FIO_Q_COMPLETED;
51                 } else
52                         io_u->error = errno;
53         }
54
55         if (io_u->error)
56                 td_verror(td, io_u->error, "xfer");
57
58         return FIO_Q_COMPLETED;
59 }
60
61 static struct ioengine_ops ioengine = {
62         .name           = "sync",
63         .version        = FIO_IOOPS_VERSION,
64         .prep           = fio_syncio_prep,
65         .queue          = fio_syncio_queue,
66         .open_file      = generic_open_file,
67         .close_file     = generic_close_file,
68         .flags          = FIO_SYNCIO,
69 };
70
71 static void fio_init fio_syncio_register(void)
72 {
73         register_ioengine(&ioengine);
74 }
75
76 static void fio_exit fio_syncio_unregister(void)
77 {
78         unregister_ioengine(&ioengine);
79 }