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