Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 1 | /* |
Jens Axboe | da751ca | 2007-03-14 10:59:33 +0100 | [diff] [blame] | 2 | * sg engine |
| 3 | * |
| 4 | * IO engine that uses the Linux SG v3 interface to talk to SCSI devices |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 5 | * |
| 6 | */ |
| 7 | #include <stdio.h> |
| 8 | #include <stdlib.h> |
| 9 | #include <unistd.h> |
| 10 | #include <errno.h> |
| 11 | #include <assert.h> |
| 12 | #include <sys/poll.h> |
Jens Axboe | 5f35095 | 2006-11-07 15:20:59 +0100 | [diff] [blame] | 13 | |
| 14 | #include "../fio.h" |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 15 | |
Jens Axboe | 34cfcda | 2006-11-03 14:00:45 +0100 | [diff] [blame] | 16 | #ifdef FIO_HAVE_SGIO |
| 17 | |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 18 | struct sgio_cmd { |
| 19 | unsigned char cdb[10]; |
| 20 | int nr; |
| 21 | }; |
| 22 | |
| 23 | struct sgio_data { |
| 24 | struct sgio_cmd *cmds; |
| 25 | struct io_u **events; |
Jens Axboe | dc0deca | 2007-02-08 20:59:31 +0100 | [diff] [blame] | 26 | struct pollfd *pfds; |
| 27 | int *fd_flags; |
| 28 | void *sgbuf; |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 29 | unsigned int bs; |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 30 | int type_checked; |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 31 | }; |
| 32 | |
| 33 | static void sgio_hdr_init(struct sgio_data *sd, struct sg_io_hdr *hdr, |
| 34 | struct io_u *io_u, int fs) |
| 35 | { |
| 36 | struct sgio_cmd *sc = &sd->cmds[io_u->index]; |
| 37 | |
| 38 | memset(hdr, 0, sizeof(*hdr)); |
| 39 | memset(sc->cdb, 0, sizeof(sc->cdb)); |
| 40 | |
| 41 | hdr->interface_id = 'S'; |
| 42 | hdr->cmdp = sc->cdb; |
| 43 | hdr->cmd_len = sizeof(sc->cdb); |
| 44 | hdr->pack_id = io_u->index; |
| 45 | hdr->usr_ptr = io_u; |
| 46 | |
| 47 | if (fs) { |
Jens Axboe | cec6b55 | 2007-02-06 20:15:38 +0100 | [diff] [blame] | 48 | hdr->dxferp = io_u->xfer_buf; |
| 49 | hdr->dxfer_len = io_u->xfer_buflen; |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 50 | } |
| 51 | } |
| 52 | |
Jens Axboe | adee86c | 2007-02-08 13:04:36 +0100 | [diff] [blame] | 53 | static int pollin_events(struct pollfd *pfds, int fds) |
| 54 | { |
| 55 | int i; |
| 56 | |
| 57 | for (i = 0; i < fds; i++) |
| 58 | if (pfds[i].revents & POLLIN) |
| 59 | return 1; |
| 60 | |
| 61 | return 0; |
| 62 | } |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 63 | |
Jens Axboe | e7d2e61 | 2007-12-11 10:49:39 +0100 | [diff] [blame] | 64 | static int fio_sgio_getevents(struct thread_data *td, unsigned int min, |
| 65 | unsigned int max, struct timespec fio_unused *t) |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 66 | { |
| 67 | struct sgio_data *sd = td->io_ops->data; |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 68 | int left = max, ret, r = 0; |
Jens Axboe | dc0deca | 2007-02-08 20:59:31 +0100 | [diff] [blame] | 69 | void *buf = sd->sgbuf; |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 70 | unsigned int i, events; |
Jens Axboe | 946ff86 | 2007-04-02 11:04:47 +0200 | [diff] [blame] | 71 | struct fio_file *f; |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 72 | |
| 73 | /* |
Jens Axboe | adee86c | 2007-02-08 13:04:36 +0100 | [diff] [blame] | 74 | * Fill in the file descriptors |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 75 | */ |
Jens Axboe | adee86c | 2007-02-08 13:04:36 +0100 | [diff] [blame] | 76 | for_each_file(td, f, i) { |
| 77 | /* |
| 78 | * don't block for min events == 0 |
| 79 | */ |
| 80 | if (!min) { |
Jens Axboe | dc0deca | 2007-02-08 20:59:31 +0100 | [diff] [blame] | 81 | sd->fd_flags[i] = fcntl(f->fd, F_GETFL); |
| 82 | fcntl(f->fd, F_SETFL, sd->fd_flags[i] | O_NONBLOCK); |
Jens Axboe | adee86c | 2007-02-08 13:04:36 +0100 | [diff] [blame] | 83 | } |
Jens Axboe | dc0deca | 2007-02-08 20:59:31 +0100 | [diff] [blame] | 84 | sd->pfds[i].fd = f->fd; |
| 85 | sd->pfds[i].events = POLLIN; |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | while (left) { |
Jens Axboe | adee86c | 2007-02-08 13:04:36 +0100 | [diff] [blame] | 89 | void *p; |
| 90 | |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 91 | do { |
| 92 | if (!min) |
| 93 | break; |
Jens Axboe | adee86c | 2007-02-08 13:04:36 +0100 | [diff] [blame] | 94 | |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 95 | ret = poll(sd->pfds, td->o.nr_files, -1); |
Jens Axboe | adee86c | 2007-02-08 13:04:36 +0100 | [diff] [blame] | 96 | if (ret < 0) { |
Jens Axboe | adee86c | 2007-02-08 13:04:36 +0100 | [diff] [blame] | 97 | if (!r) |
Jens Axboe | 22819ec | 2007-02-18 07:47:14 +0100 | [diff] [blame] | 98 | r = -errno; |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 99 | td_verror(td, errno, "poll"); |
Jens Axboe | adee86c | 2007-02-08 13:04:36 +0100 | [diff] [blame] | 100 | break; |
| 101 | } else if (!ret) |
| 102 | continue; |
| 103 | |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 104 | if (pollin_events(sd->pfds, td->o.nr_files)) |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 105 | break; |
| 106 | } while (1); |
| 107 | |
Jens Axboe | adee86c | 2007-02-08 13:04:36 +0100 | [diff] [blame] | 108 | if (r < 0) |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 109 | break; |
| 110 | |
Jens Axboe | adee86c | 2007-02-08 13:04:36 +0100 | [diff] [blame] | 111 | re_read: |
| 112 | p = buf; |
| 113 | events = 0; |
| 114 | for_each_file(td, f, i) { |
| 115 | ret = read(f->fd, p, left * sizeof(struct sg_io_hdr)); |
| 116 | if (ret < 0) { |
| 117 | if (errno == EAGAIN) |
| 118 | continue; |
Jens Axboe | 22819ec | 2007-02-18 07:47:14 +0100 | [diff] [blame] | 119 | r = -errno; |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 120 | td_verror(td, errno, "read"); |
Jens Axboe | adee86c | 2007-02-08 13:04:36 +0100 | [diff] [blame] | 121 | break; |
| 122 | } else if (ret) { |
| 123 | p += ret; |
| 124 | events += ret / sizeof(struct sg_io_hdr); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | if (r < 0) |
| 129 | break; |
| 130 | if (!events) { |
| 131 | usleep(1000); |
| 132 | goto re_read; |
| 133 | } |
| 134 | |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 135 | left -= events; |
| 136 | r += events; |
| 137 | |
| 138 | for (i = 0; i < events; i++) { |
| 139 | struct sg_io_hdr *hdr = (struct sg_io_hdr *) buf + i; |
| 140 | |
| 141 | sd->events[i] = hdr->usr_ptr; |
| 142 | } |
| 143 | } |
| 144 | |
Jens Axboe | adee86c | 2007-02-08 13:04:36 +0100 | [diff] [blame] | 145 | if (!min) { |
| 146 | for_each_file(td, f, i) |
Jens Axboe | dc0deca | 2007-02-08 20:59:31 +0100 | [diff] [blame] | 147 | fcntl(f->fd, F_SETFL, sd->fd_flags[i]); |
Jens Axboe | adee86c | 2007-02-08 13:04:36 +0100 | [diff] [blame] | 148 | } |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 149 | |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 150 | return r; |
| 151 | } |
| 152 | |
Jens Axboe | 7a16dd0 | 2006-10-18 17:21:58 +0200 | [diff] [blame] | 153 | static int fio_sgio_ioctl_doio(struct thread_data *td, |
| 154 | struct fio_file *f, struct io_u *io_u) |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 155 | { |
| 156 | struct sgio_data *sd = td->io_ops->data; |
| 157 | struct sg_io_hdr *hdr = &io_u->hdr; |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 158 | int ret; |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 159 | |
| 160 | sd->events[0] = io_u; |
| 161 | |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 162 | ret = ioctl(f->fd, SG_IO, hdr); |
| 163 | if (ret < 0) |
Jens Axboe | a05bd42 | 2007-04-02 10:42:33 +0200 | [diff] [blame] | 164 | return ret; |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 165 | |
| 166 | return FIO_Q_COMPLETED; |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 167 | } |
| 168 | |
Jens Axboe | 2b13e71 | 2011-01-19 14:04:16 -0700 | [diff] [blame] | 169 | static int fio_sgio_rw_doio(struct fio_file *f, struct io_u *io_u, int do_sync) |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 170 | { |
| 171 | struct sg_io_hdr *hdr = &io_u->hdr; |
| 172 | int ret; |
| 173 | |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 174 | ret = write(f->fd, hdr, sizeof(*hdr)); |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 175 | if (ret < 0) |
Jens Axboe | a05bd42 | 2007-04-02 10:42:33 +0200 | [diff] [blame] | 176 | return ret; |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 177 | |
Jens Axboe | 2b13e71 | 2011-01-19 14:04:16 -0700 | [diff] [blame] | 178 | if (do_sync) { |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 179 | ret = read(f->fd, hdr, sizeof(*hdr)); |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 180 | if (ret < 0) |
Jens Axboe | a05bd42 | 2007-04-02 10:42:33 +0200 | [diff] [blame] | 181 | return ret; |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 182 | return FIO_Q_COMPLETED; |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 183 | } |
| 184 | |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 185 | return FIO_Q_QUEUED; |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 186 | } |
| 187 | |
Jens Axboe | 2b13e71 | 2011-01-19 14:04:16 -0700 | [diff] [blame] | 188 | static int fio_sgio_doio(struct thread_data *td, struct io_u *io_u, int do_sync) |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 189 | { |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 190 | struct fio_file *f = io_u->file; |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 191 | |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 192 | if (f->filetype == FIO_TYPE_BD) |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 193 | return fio_sgio_ioctl_doio(td, f, io_u); |
| 194 | |
Jens Axboe | 2b13e71 | 2011-01-19 14:04:16 -0700 | [diff] [blame] | 195 | return fio_sgio_rw_doio(f, io_u, do_sync); |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 196 | } |
| 197 | |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 198 | static int fio_sgio_prep(struct thread_data *td, struct io_u *io_u) |
| 199 | { |
| 200 | struct sg_io_hdr *hdr = &io_u->hdr; |
| 201 | struct sgio_data *sd = td->io_ops->data; |
| 202 | int nr_blocks, lba; |
| 203 | |
Jens Axboe | cec6b55 | 2007-02-06 20:15:38 +0100 | [diff] [blame] | 204 | if (io_u->xfer_buflen & (sd->bs - 1)) { |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 205 | log_err("read/write not sector aligned\n"); |
| 206 | return EINVAL; |
| 207 | } |
| 208 | |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 209 | if (io_u->ddir == DDIR_READ) { |
Jens Axboe | 87dc1ab | 2006-10-24 14:41:26 +0200 | [diff] [blame] | 210 | sgio_hdr_init(sd, hdr, io_u, 1); |
| 211 | |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 212 | hdr->dxfer_direction = SG_DXFER_FROM_DEV; |
| 213 | hdr->cmdp[0] = 0x28; |
Jens Axboe | 87dc1ab | 2006-10-24 14:41:26 +0200 | [diff] [blame] | 214 | } else if (io_u->ddir == DDIR_WRITE) { |
| 215 | sgio_hdr_init(sd, hdr, io_u, 1); |
| 216 | |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 217 | hdr->dxfer_direction = SG_DXFER_TO_DEV; |
| 218 | hdr->cmdp[0] = 0x2a; |
Jens Axboe | 87dc1ab | 2006-10-24 14:41:26 +0200 | [diff] [blame] | 219 | } else { |
| 220 | sgio_hdr_init(sd, hdr, io_u, 0); |
| 221 | |
| 222 | hdr->dxfer_direction = SG_DXFER_NONE; |
| 223 | hdr->cmdp[0] = 0x35; |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 224 | } |
| 225 | |
Jens Axboe | 87dc1ab | 2006-10-24 14:41:26 +0200 | [diff] [blame] | 226 | if (hdr->dxfer_direction != SG_DXFER_NONE) { |
Jens Axboe | cec6b55 | 2007-02-06 20:15:38 +0100 | [diff] [blame] | 227 | nr_blocks = io_u->xfer_buflen / sd->bs; |
Jens Axboe | 87dc1ab | 2006-10-24 14:41:26 +0200 | [diff] [blame] | 228 | lba = io_u->offset / sd->bs; |
Jens Axboe | 1e97cce | 2006-12-05 11:44:16 +0100 | [diff] [blame] | 229 | hdr->cmdp[2] = (unsigned char) ((lba >> 24) & 0xff); |
| 230 | hdr->cmdp[3] = (unsigned char) ((lba >> 16) & 0xff); |
| 231 | hdr->cmdp[4] = (unsigned char) ((lba >> 8) & 0xff); |
| 232 | hdr->cmdp[5] = (unsigned char) (lba & 0xff); |
| 233 | hdr->cmdp[7] = (unsigned char) ((nr_blocks >> 8) & 0xff); |
| 234 | hdr->cmdp[8] = (unsigned char) (nr_blocks & 0xff); |
Jens Axboe | 87dc1ab | 2006-10-24 14:41:26 +0200 | [diff] [blame] | 235 | } |
| 236 | |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 237 | return 0; |
| 238 | } |
| 239 | |
| 240 | static int fio_sgio_queue(struct thread_data *td, struct io_u *io_u) |
| 241 | { |
| 242 | struct sg_io_hdr *hdr = &io_u->hdr; |
Jens Axboe | f6db4fa | 2009-06-16 23:23:01 +0200 | [diff] [blame] | 243 | int ret, do_sync = 0; |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 244 | |
Jens Axboe | 7101d9c | 2007-09-12 13:12:39 +0200 | [diff] [blame] | 245 | fio_ro_check(td, io_u); |
| 246 | |
Jens Axboe | f6db4fa | 2009-06-16 23:23:01 +0200 | [diff] [blame] | 247 | if (td->o.sync_io || td->o.odirect || ddir_sync(io_u->ddir)) |
| 248 | do_sync = 1; |
| 249 | |
| 250 | ret = fio_sgio_doio(td, io_u, do_sync); |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 251 | |
| 252 | if (ret < 0) |
| 253 | io_u->error = errno; |
| 254 | else if (hdr->status) { |
| 255 | io_u->resid = hdr->resid; |
| 256 | io_u->error = EIO; |
| 257 | } |
| 258 | |
Jens Axboe | 95bcd81 | 2007-02-11 01:01:57 +0100 | [diff] [blame] | 259 | if (io_u->error) { |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 260 | td_verror(td, io_u->error, "xfer"); |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 261 | return FIO_Q_COMPLETED; |
Jens Axboe | 95bcd81 | 2007-02-11 01:01:57 +0100 | [diff] [blame] | 262 | } |
| 263 | |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 264 | return ret; |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | static struct io_u *fio_sgio_event(struct thread_data *td, int event) |
| 268 | { |
| 269 | struct sgio_data *sd = td->io_ops->data; |
| 270 | |
| 271 | return sd->events[event]; |
| 272 | } |
| 273 | |
| 274 | static int fio_sgio_get_bs(struct thread_data *td, unsigned int *bs) |
| 275 | { |
| 276 | struct sgio_data *sd = td->io_ops->data; |
Jens Axboe | 2039ab1 | 2010-08-27 09:04:00 +0200 | [diff] [blame] | 277 | struct io_u io_u; |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 278 | struct sg_io_hdr *hdr; |
| 279 | unsigned char buf[8]; |
| 280 | int ret; |
| 281 | |
Jens Axboe | 2039ab1 | 2010-08-27 09:04:00 +0200 | [diff] [blame] | 282 | memset(&io_u, 0, sizeof(io_u)); |
| 283 | io_u.file = td->files[0]; |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 284 | |
Jens Axboe | 2039ab1 | 2010-08-27 09:04:00 +0200 | [diff] [blame] | 285 | hdr = &io_u.hdr; |
| 286 | sgio_hdr_init(sd, hdr, &io_u, 0); |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 287 | memset(buf, 0, sizeof(buf)); |
| 288 | |
| 289 | hdr->cmdp[0] = 0x25; |
| 290 | hdr->dxfer_direction = SG_DXFER_FROM_DEV; |
| 291 | hdr->dxferp = buf; |
| 292 | hdr->dxfer_len = sizeof(buf); |
| 293 | |
Jens Axboe | 2039ab1 | 2010-08-27 09:04:00 +0200 | [diff] [blame] | 294 | ret = fio_sgio_doio(td, &io_u, 1); |
| 295 | if (ret) |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 296 | return ret; |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 297 | |
| 298 | *bs = (buf[4] << 24) | (buf[5] << 16) | (buf[6] << 8) | buf[7]; |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 299 | return 0; |
| 300 | } |
| 301 | |
| 302 | static void fio_sgio_cleanup(struct thread_data *td) |
| 303 | { |
Jens Axboe | dc0deca | 2007-02-08 20:59:31 +0100 | [diff] [blame] | 304 | struct sgio_data *sd = td->io_ops->data; |
| 305 | |
| 306 | if (sd) { |
| 307 | free(sd->events); |
| 308 | free(sd->cmds); |
| 309 | free(sd->fd_flags); |
| 310 | free(sd->pfds); |
| 311 | free(sd->sgbuf); |
| 312 | free(sd); |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 313 | } |
| 314 | } |
| 315 | |
| 316 | static int fio_sgio_init(struct thread_data *td) |
| 317 | { |
| 318 | struct sgio_data *sd; |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 319 | |
| 320 | sd = malloc(sizeof(*sd)); |
Jens Axboe | cb781c7 | 2006-11-07 14:02:48 +0100 | [diff] [blame] | 321 | memset(sd, 0, sizeof(*sd)); |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 322 | sd->cmds = malloc(td->o.iodepth * sizeof(struct sgio_cmd)); |
| 323 | memset(sd->cmds, 0, td->o.iodepth * sizeof(struct sgio_cmd)); |
| 324 | sd->events = malloc(td->o.iodepth * sizeof(struct io_u *)); |
| 325 | memset(sd->events, 0, td->o.iodepth * sizeof(struct io_u *)); |
| 326 | sd->pfds = malloc(sizeof(struct pollfd) * td->o.nr_files); |
| 327 | memset(sd->pfds, 0, sizeof(struct pollfd) * td->o.nr_files); |
| 328 | sd->fd_flags = malloc(sizeof(int) * td->o.nr_files); |
| 329 | memset(sd->fd_flags, 0, sizeof(int) * td->o.nr_files); |
| 330 | sd->sgbuf = malloc(sizeof(struct sg_io_hdr) * td->o.iodepth); |
| 331 | memset(sd->sgbuf, 0, sizeof(struct sg_io_hdr) * td->o.iodepth); |
Jens Axboe | dc0deca | 2007-02-08 20:59:31 +0100 | [diff] [blame] | 332 | |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 333 | td->io_ops->data = sd; |
| 334 | |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 335 | /* |
| 336 | * we want to do it, regardless of whether odirect is set or not |
| 337 | */ |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 338 | td->o.override_sync = 1; |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 339 | return 0; |
| 340 | } |
| 341 | |
| 342 | static int fio_sgio_type_check(struct thread_data *td, struct fio_file *f) |
| 343 | { |
| 344 | struct sgio_data *sd = td->io_ops->data; |
| 345 | unsigned int bs; |
| 346 | |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 347 | if (f->filetype == FIO_TYPE_BD) { |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 348 | if (ioctl(f->fd, BLKSSZGET, &bs) < 0) { |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 349 | td_verror(td, errno, "ioctl"); |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 350 | return 1; |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 351 | } |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 352 | } else if (f->filetype == FIO_TYPE_CHAR) { |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 353 | int version, ret; |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 354 | |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 355 | if (ioctl(f->fd, SG_GET_VERSION_NUM, &version) < 0) { |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 356 | td_verror(td, errno, "ioctl"); |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 357 | return 1; |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | ret = fio_sgio_get_bs(td, &bs); |
| 361 | if (ret) |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 362 | return 1; |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 363 | } else { |
Gurudas Pai | d0c7093 | 2008-05-30 13:35:00 +0200 | [diff] [blame] | 364 | log_err("ioengine sg only works on block devices\n"); |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 365 | return 1; |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | sd->bs = bs; |
| 369 | |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 370 | if (f->filetype == FIO_TYPE_BD) { |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 371 | td->io_ops->getevents = NULL; |
| 372 | td->io_ops->event = NULL; |
| 373 | } |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 374 | |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 375 | return 0; |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | static int fio_sgio_open(struct thread_data *td, struct fio_file *f) |
| 379 | { |
| 380 | struct sgio_data *sd = td->io_ops->data; |
| 381 | int ret; |
| 382 | |
| 383 | ret = generic_open_file(td, f); |
| 384 | if (ret) |
| 385 | return ret; |
| 386 | |
Jens Axboe | 15ba640 | 2007-04-02 10:51:10 +0200 | [diff] [blame] | 387 | if (sd && !sd->type_checked && fio_sgio_type_check(td, f)) { |
Jens Axboe | 6977bcd | 2008-03-01 15:55:36 +0100 | [diff] [blame] | 388 | ret = generic_close_file(td, f); |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 389 | return 1; |
| 390 | } |
| 391 | |
| 392 | return 0; |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 393 | } |
| 394 | |
Jens Axboe | 5f35095 | 2006-11-07 15:20:59 +0100 | [diff] [blame] | 395 | static struct ioengine_ops ioengine = { |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 396 | .name = "sg", |
| 397 | .version = FIO_IOOPS_VERSION, |
| 398 | .init = fio_sgio_init, |
| 399 | .prep = fio_sgio_prep, |
| 400 | .queue = fio_sgio_queue, |
| 401 | .getevents = fio_sgio_getevents, |
| 402 | .event = fio_sgio_event, |
| 403 | .cleanup = fio_sgio_cleanup, |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 404 | .open_file = fio_sgio_open, |
| 405 | .close_file = generic_close_file, |
Jens Axboe | df9c26b | 2009-03-05 10:13:58 +0100 | [diff] [blame] | 406 | .get_file_size = generic_get_file_size, |
Jens Axboe | b2a1519 | 2006-10-18 14:10:42 +0200 | [diff] [blame] | 407 | .flags = FIO_SYNCIO | FIO_RAWIO, |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 408 | }; |
Jens Axboe | 34cfcda | 2006-11-03 14:00:45 +0100 | [diff] [blame] | 409 | |
| 410 | #else /* FIO_HAVE_SGIO */ |
| 411 | |
| 412 | /* |
| 413 | * When we have a proper configure system in place, we simply wont build |
| 414 | * and install this io engine. For now install a crippled version that |
| 415 | * just complains and fails to load. |
| 416 | */ |
| 417 | static int fio_sgio_init(struct thread_data fio_unused *td) |
| 418 | { |
Jens Axboe | a3edaf7 | 2010-09-26 10:53:40 +0900 | [diff] [blame] | 419 | log_err("fio: ioengine sg not available\n"); |
Jens Axboe | 34cfcda | 2006-11-03 14:00:45 +0100 | [diff] [blame] | 420 | return 1; |
| 421 | } |
| 422 | |
Jens Axboe | 5f35095 | 2006-11-07 15:20:59 +0100 | [diff] [blame] | 423 | static struct ioengine_ops ioengine = { |
Gurudas Pai | d0c7093 | 2008-05-30 13:35:00 +0200 | [diff] [blame] | 424 | .name = "sg", |
Jens Axboe | 34cfcda | 2006-11-03 14:00:45 +0100 | [diff] [blame] | 425 | .version = FIO_IOOPS_VERSION, |
| 426 | .init = fio_sgio_init, |
| 427 | }; |
| 428 | |
| 429 | #endif |
Jens Axboe | 5f35095 | 2006-11-07 15:20:59 +0100 | [diff] [blame] | 430 | |
| 431 | static void fio_init fio_sgio_register(void) |
| 432 | { |
| 433 | register_ioengine(&ioengine); |
| 434 | } |
| 435 | |
| 436 | static void fio_exit fio_sgio_unregister(void) |
| 437 | { |
| 438 | unregister_ioengine(&ioengine); |
| 439 | } |