Use POSIX path for poll.h and fcntl.h headers
[fio.git] / engines / binject.c
index f7e56eb2ed0207bc7169a3259e4c2edb758fad5b..49042a3b15843590c5500cb28d47069fc1abc7d8 100644 (file)
@@ -11,7 +11,7 @@
 #include <errno.h>
 #include <assert.h>
 #include <string.h>
-#include <sys/poll.h>
+#include <poll.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 
@@ -59,17 +59,18 @@ static int pollin_events(struct pollfd *pfds, int fds)
        return 0;
 }
 
-static unsigned int binject_read_commands(struct thread_data *td, void *p,
+static unsigned int binject_read_commands(struct thread_data *td, void *buf,
                                          int left, int *err)
 {
-       struct binject_file *bf;
        struct fio_file *f;
        int i, ret, events;
+       char *p = buf;
 
 one_more:
        events = 0;
        for_each_file(td, f, i) {
-               bf = f->file_data;
+               struct binject_file *bf = FILE_ENG_DATA(f);
+
                ret = read(bf->fd, p, left * sizeof(struct b_user_cmd));
                if (ret < 0) {
                        if (errno == EAGAIN)
@@ -91,28 +92,29 @@ one_more:
 }
 
 static int fio_binject_getevents(struct thread_data *td, unsigned int min,
-                             unsigned int max, struct timespec fio_unused *t)
+                                unsigned int max,
+                                const struct timespec fio_unused *t)
 {
-       struct binject_data *bd = td->io_ops->data;
+       struct binject_data *bd = td->io_ops_data;
        int left = max, ret, r = 0, ev_index = 0;
        void *buf = bd->cmds;
        unsigned int i, events;
        struct fio_file *f;
-       struct binject_file *bf;
 
        /*
         * Fill in the file descriptors
         */
        for_each_file(td, f, i) {
-               bf = f->file_data;
+               struct binject_file *bf = FILE_ENG_DATA(f);
 
                /*
                 * don't block for min events == 0
                 */
-               if (!min) {
-                       bd->fd_flags[i] = fcntl(bf->fd, F_GETFL);
-                       fcntl(bf->fd, F_SETFL, bd->fd_flags[i] | O_NONBLOCK);
-               }
+               if (!min)
+                       bd->fd_flags[i] = fio_set_fd_nonblocking(bf->fd, "binject");
+               else
+                       bd->fd_flags[i] = -1;
+
                bd->pfds[i].fd = bf->fd;
                bd->pfds[i].events = POLLIN;
        }
@@ -153,8 +155,13 @@ static int fio_binject_getevents(struct thread_data *td, unsigned int min,
 
        if (!min) {
                for_each_file(td, f, i) {
-                       bf = f->file_data;
-                       fcntl(bf->fd, F_SETFL, bd->fd_flags[i]);
+                       struct binject_file *bf = FILE_ENG_DATA(f);
+
+                       if (bd->fd_flags[i] == -1)
+                               continue;
+
+                       if (fcntl(bf->fd, F_SETFL, bd->fd_flags[i]) < 0)
+                               log_err("fio: binject failed to restore fcntl flags: %s\n", strerror(errno));
                }
        }
 
@@ -167,7 +174,7 @@ static int fio_binject_getevents(struct thread_data *td, unsigned int min,
 static int fio_binject_doio(struct thread_data *td, struct io_u *io_u)
 {
        struct b_user_cmd *buc = &io_u->buc;
-       struct binject_file *bf = io_u->file->file_data;
+       struct binject_file *bf = FILE_ENG_DATA(io_u->file);
        int ret;
 
        ret = write(bf->fd, buc, sizeof(*buc));
@@ -179,9 +186,9 @@ static int fio_binject_doio(struct thread_data *td, struct io_u *io_u)
 
 static int fio_binject_prep(struct thread_data *td, struct io_u *io_u)
 {
-       struct binject_data *bd = td->io_ops->data;
+       struct binject_data *bd = td->io_ops_data;
        struct b_user_cmd *buc = &io_u->buc;
-       struct binject_file *bf = io_u->file->file_data;
+       struct binject_file *bf = FILE_ENG_DATA(io_u->file);
 
        if (io_u->xfer_buflen & (bf->bs - 1)) {
                log_err("read/write not sector aligned\n");
@@ -228,11 +235,22 @@ static int fio_binject_queue(struct thread_data *td, struct io_u *io_u)
 
 static struct io_u *fio_binject_event(struct thread_data *td, int event)
 {
-       struct binject_data *bd = td->io_ops->data;
+       struct binject_data *bd = td->io_ops_data;
 
        return bd->events[event];
 }
 
+static int binject_open_ctl(struct thread_data *td)
+{
+       int fd;
+
+       fd = open("/dev/binject-ctl", O_RDWR);
+       if (fd < 0)
+               td_verror(td, errno, "open binject-ctl");
+
+       return fd;
+}
+
 static void binject_unmap_dev(struct thread_data *td, struct binject_file *bf)
 {
        struct b_ioctl_cmd bic;
@@ -243,19 +261,14 @@ static void binject_unmap_dev(struct thread_data *td, struct binject_file *bf)
                bf->fd = -1;
        }
 
-       fdb = open("/dev/binject-ctl", O_RDWR);
-       if (fdb < 0) {
-               td_verror(td, errno, "open binject-ctl");
+       fdb = binject_open_ctl(td);
+       if (fdb < 0)
                return;
-       }
 
        bic.minor = bf->minor;
 
-       if (ioctl(fdb, 1, &bic) < 0) {
+       if (ioctl(fdb, B_IOCTL_DEL, &bic) < 0)
                td_verror(td, errno, "binject dev unmap");
-               close(fdb);
-               return;
-       }
 
        close(fdb);
 }
@@ -268,15 +281,13 @@ static int binject_map_dev(struct thread_data *td, struct binject_file *bf,
        struct stat sb;
        int fdb, dev_there, loops;
 
-       fdb = open("/dev/binject-ctl", O_RDWR);
-       if (fdb < 0) {
-               td_verror(td, errno, "binject ctl open");
+       fdb = binject_open_ctl(td);
+       if (fdb < 0)
                return 1;
-       }
 
        bic.fd = fd;
 
-       if (ioctl(fdb, 0, &bic) < 0) {
+       if (ioctl(fdb, B_IOCTL_ADD, &bic) < 0) {
                td_verror(td, errno, "binject dev map");
                close(fdb);
                return 1;
@@ -319,12 +330,12 @@ err_unmap:
 
 static int fio_binject_close_file(struct thread_data *td, struct fio_file *f)
 {
-       struct binject_file *bf = f->file_data;
+       struct binject_file *bf = FILE_ENG_DATA(f);
 
        if (bf) {
                binject_unmap_dev(td, bf);
                free(bf);
-               f->file_data = NULL;
+               FILE_SET_ENG_DATA(f, NULL);
                return generic_close_file(td, f);
        }
 
@@ -341,7 +352,7 @@ static int fio_binject_open_file(struct thread_data *td, struct fio_file *f)
        if (ret)
                return 1;
 
-       if (f->filetype != FIO_TYPE_BD) {
+       if (f->filetype != FIO_TYPE_BLOCK) {
                log_err("fio: binject only works with block devices\n");
                goto err_close;
        }
@@ -353,7 +364,7 @@ static int fio_binject_open_file(struct thread_data *td, struct fio_file *f)
        bf = malloc(sizeof(*bf));
        bf->bs = bs;
        bf->minor = bf->fd = -1;
-       f->file_data = bf;
+       FILE_SET_ENG_DATA(f, bf);
 
        if (binject_map_dev(td, bf, f->fd)) {
 err_close:
@@ -366,7 +377,7 @@ err_close:
 
 static void fio_binject_cleanup(struct thread_data *td)
 {
-       struct binject_data *bd = td->io_ops->data;
+       struct binject_data *bd = td->io_ops_data;
 
        if (bd) {
                free(bd->events);
@@ -396,7 +407,7 @@ static int fio_binject_init(struct thread_data *td)
        bd->fd_flags = malloc(sizeof(int) * td->o.nr_files);
        memset(bd->fd_flags, 0, sizeof(int) * td->o.nr_files);
 
-       td->io_ops->data = bd;
+       td->io_ops_data = bd;
        return 0;
 }
 
@@ -412,7 +423,7 @@ static struct ioengine_ops ioengine = {
        .open_file      = fio_binject_open_file,
        .close_file     = fio_binject_close_file,
        .get_file_size  = generic_get_file_size,
-       .flags          = FIO_RAWIO | FIO_BARRIER,
+       .flags          = FIO_RAWIO | FIO_BARRIER | FIO_MEMALIGN,
 };
 
 #else /* FIO_HAVE_BINJECT */