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