Fix integer overflow in rate_iops
[fio.git] / engines / sg.c
index bc82b09c5635b487520f70193f9c127355892d24..8acbd50adbe0ccecb98c886ed718aa74941870a8 100644 (file)
@@ -62,7 +62,8 @@ static int pollin_events(struct pollfd *pfds, int fds)
 }
 
 static int fio_sgio_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 sgio_data *sd = td->io_ops->data;
        int left = max, ret, r = 0;
@@ -77,10 +78,11 @@ static int fio_sgio_getevents(struct thread_data *td, unsigned int min,
                /*
                 * don't block for min events == 0
                 */
-               if (!min) {
-                       sd->fd_flags[i] = fcntl(f->fd, F_GETFL);
-                       fcntl(f->fd, F_SETFL, sd->fd_flags[i] | O_NONBLOCK);
-               }
+               if (!min)
+                       sd->fd_flags[i] = fio_set_fd_nonblocking(f->fd, "sg");
+               else
+                       sd->fd_flags[i] = -1;
+
                sd->pfds[i].fd = f->fd;
                sd->pfds[i].events = POLLIN;
        }
@@ -143,8 +145,13 @@ re_read:
        }
 
        if (!min) {
-               for_each_file(td, f, i)
-                       fcntl(f->fd, F_SETFL, sd->fd_flags[i]);
+               for_each_file(td, f, i) {
+                       if (sd->fd_flags[i] == -1)
+                               continue;
+
+                       if (fcntl(f->fd, F_SETFL, sd->fd_flags[i]) < 0)
+                               log_err("fio: sg failed to restore fcntl flags: %s\n", strerror(errno));
+               }
        }
 
        return r;
@@ -166,7 +173,7 @@ static int fio_sgio_ioctl_doio(struct thread_data *td,
        return FIO_Q_COMPLETED;
 }
 
-static int fio_sgio_rw_doio(struct fio_file *f, struct io_u *io_u, int sync)
+static int fio_sgio_rw_doio(struct fio_file *f, struct io_u *io_u, int do_sync)
 {
        struct sg_io_hdr *hdr = &io_u->hdr;
        int ret;
@@ -175,7 +182,7 @@ static int fio_sgio_rw_doio(struct fio_file *f, struct io_u *io_u, int sync)
        if (ret < 0)
                return ret;
 
-       if (sync) {
+       if (do_sync) {
                ret = read(f->fd, hdr, sizeof(*hdr));
                if (ret < 0)
                        return ret;
@@ -185,14 +192,14 @@ static int fio_sgio_rw_doio(struct fio_file *f, struct io_u *io_u, int sync)
        return FIO_Q_QUEUED;
 }
 
-static int fio_sgio_doio(struct thread_data *td, struct io_u *io_u, int sync)
+static int fio_sgio_doio(struct thread_data *td, struct io_u *io_u, int do_sync)
 {
        struct fio_file *f = io_u->file;
 
        if (f->filetype == FIO_TYPE_BD)
                return fio_sgio_ioctl_doio(td, f, io_u);
 
-       return fio_sgio_rw_doio(f, io_u, sync);
+       return fio_sgio_rw_doio(f, io_u, do_sync);
 }
 
 static int fio_sgio_prep(struct thread_data *td, struct io_u *io_u)
@@ -361,6 +368,7 @@ static int fio_sgio_type_check(struct thread_data *td, struct fio_file *f)
                if (ret)
                        return 1;
        } else {
+               td_verror(td, EINVAL, "wrong file type");
                log_err("ioengine sg only works on block devices\n");
                return 1;
        }
@@ -416,7 +424,7 @@ static struct ioengine_ops ioengine = {
  */
 static int fio_sgio_init(struct thread_data fio_unused *td)
 {
-       fprintf(stderr, "fio: ioengine sg not available\n");
+       log_err("fio: ioengine sg not available\n");
        return 1;
 }