4 * IO engine that uses the Linux SG v3 interface to talk to SCSI devices
20 unsigned char cdb[10];
25 struct sgio_cmd *cmds;
34 static void sgio_hdr_init(struct sgio_data *sd, struct sg_io_hdr *hdr,
35 struct io_u *io_u, int fs)
37 struct sgio_cmd *sc = &sd->cmds[io_u->index];
39 memset(hdr, 0, sizeof(*hdr));
40 memset(sc->cdb, 0, sizeof(sc->cdb));
42 hdr->interface_id = 'S';
44 hdr->cmd_len = sizeof(sc->cdb);
45 hdr->pack_id = io_u->index;
49 hdr->dxferp = io_u->xfer_buf;
50 hdr->dxfer_len = io_u->xfer_buflen;
54 static int pollin_events(struct pollfd *pfds, int fds)
58 for (i = 0; i < fds; i++)
59 if (pfds[i].revents & POLLIN)
65 static int fio_sgio_getevents(struct thread_data *td, int min, int max,
66 struct timespec fio_unused *t)
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.
72 struct fio_file *f = &td->files[0];
73 struct sgio_data *sd = td->io_ops->data;
74 int left = max, ret, r = 0;
75 void *buf = sd->sgbuf;
76 unsigned int i, events;
79 * Fill in the file descriptors
81 for_each_file(td, f, i) {
83 * don't block for min events == 0
86 sd->fd_flags[i] = fcntl(f->fd, F_GETFL);
87 fcntl(f->fd, F_SETFL, sd->fd_flags[i] | O_NONBLOCK);
89 sd->pfds[i].fd = f->fd;
90 sd->pfds[i].events = POLLIN;
100 ret = poll(sd->pfds, td->o.nr_files, -1);
104 td_verror(td, errno, "poll");
109 if (pollin_events(sd->pfds, td->o.nr_files))
119 for_each_file(td, f, i) {
120 ret = read(f->fd, p, left * sizeof(struct sg_io_hdr));
125 td_verror(td, errno, "read");
129 events += ret / sizeof(struct sg_io_hdr);
143 for (i = 0; i < events; i++) {
144 struct sg_io_hdr *hdr = (struct sg_io_hdr *) buf + i;
146 sd->events[i] = hdr->usr_ptr;
151 for_each_file(td, f, i)
152 fcntl(f->fd, F_SETFL, sd->fd_flags[i]);
158 static int fio_sgio_ioctl_doio(struct thread_data *td,
159 struct fio_file *f, struct io_u *io_u)
161 struct sgio_data *sd = td->io_ops->data;
162 struct sg_io_hdr *hdr = &io_u->hdr;
165 sd->events[0] = io_u;
167 ret = ioctl(f->fd, SG_IO, hdr);
171 return FIO_Q_COMPLETED;
174 static int fio_sgio_rw_doio(struct fio_file *f, struct io_u *io_u, int sync)
176 struct sg_io_hdr *hdr = &io_u->hdr;
179 ret = write(f->fd, hdr, sizeof(*hdr));
184 ret = read(f->fd, hdr, sizeof(*hdr));
187 return FIO_Q_COMPLETED;
193 static int fio_sgio_doio(struct thread_data *td, struct io_u *io_u, int sync)
195 struct fio_file *f = io_u->file;
197 if (f->filetype == FIO_TYPE_BD)
198 return fio_sgio_ioctl_doio(td, f, io_u);
200 return fio_sgio_rw_doio(f, io_u, sync);
203 static int fio_sgio_prep(struct thread_data *td, struct io_u *io_u)
205 struct sg_io_hdr *hdr = &io_u->hdr;
206 struct sgio_data *sd = td->io_ops->data;
209 if (io_u->xfer_buflen & (sd->bs - 1)) {
210 log_err("read/write not sector aligned\n");
214 if (io_u->ddir == DDIR_READ) {
215 sgio_hdr_init(sd, hdr, io_u, 1);
217 hdr->dxfer_direction = SG_DXFER_FROM_DEV;
219 } else if (io_u->ddir == DDIR_WRITE) {
220 sgio_hdr_init(sd, hdr, io_u, 1);
222 hdr->dxfer_direction = SG_DXFER_TO_DEV;
225 sgio_hdr_init(sd, hdr, io_u, 0);
227 hdr->dxfer_direction = SG_DXFER_NONE;
231 if (hdr->dxfer_direction != SG_DXFER_NONE) {
232 nr_blocks = io_u->xfer_buflen / sd->bs;
233 lba = io_u->offset / sd->bs;
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);
245 static int fio_sgio_queue(struct thread_data *td, struct io_u *io_u)
247 struct sg_io_hdr *hdr = &io_u->hdr;
250 ret = fio_sgio_doio(td, io_u, io_u->ddir == DDIR_SYNC);
254 else if (hdr->status) {
255 io_u->resid = hdr->resid;
260 td_verror(td, io_u->error, "xfer");
261 return FIO_Q_COMPLETED;
267 static struct io_u *fio_sgio_event(struct thread_data *td, int event)
269 struct sgio_data *sd = td->io_ops->data;
271 return sd->events[event];
274 static int fio_sgio_get_bs(struct thread_data *td, unsigned int *bs)
276 struct sgio_data *sd = td->io_ops->data;
278 struct sg_io_hdr *hdr;
279 unsigned char buf[8];
282 io_u = __get_io_u(td);
283 io_u->file = &td->files[0];
287 sgio_hdr_init(sd, hdr, io_u, 0);
288 memset(buf, 0, sizeof(buf));
291 hdr->dxfer_direction = SG_DXFER_FROM_DEV;
293 hdr->dxfer_len = sizeof(buf);
295 ret = fio_sgio_doio(td, io_u, 1);
301 *bs = (buf[4] << 24) | (buf[5] << 16) | (buf[6] << 8) | buf[7];
306 static void fio_sgio_cleanup(struct thread_data *td)
308 struct sgio_data *sd = td->io_ops->data;
318 td->io_ops->data = NULL;
322 static int fio_sgio_init(struct thread_data *td)
324 struct sgio_data *sd;
326 sd = malloc(sizeof(*sd));
327 memset(sd, 0, sizeof(*sd));
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);
339 td->io_ops->data = sd;
342 * we want to do it, regardless of whether odirect is set or not
344 td->o.override_sync = 1;
348 static int fio_sgio_type_check(struct thread_data *td, struct fio_file *f)
350 struct sgio_data *sd = td->io_ops->data;
353 if (f->filetype == FIO_TYPE_BD) {
354 if (ioctl(f->fd, BLKSSZGET, &bs) < 0) {
355 td_verror(td, errno, "ioctl");
358 } else if (f->filetype == FIO_TYPE_CHAR) {
361 if (ioctl(f->fd, SG_GET_VERSION_NUM, &version) < 0) {
362 td_verror(td, errno, "ioctl");
366 ret = fio_sgio_get_bs(td, &bs);
370 log_err("ioengine sgio only works on block devices\n");
376 if (f->filetype == FIO_TYPE_BD) {
377 td->io_ops->getevents = NULL;
378 td->io_ops->event = NULL;
384 static int fio_sgio_open(struct thread_data *td, struct fio_file *f)
386 struct sgio_data *sd = td->io_ops->data;
389 ret = generic_open_file(td, f);
393 if (!sd->type_checked && fio_sgio_type_check(td, f)) {
394 generic_close_file(td, f);
401 static struct ioengine_ops ioengine = {
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,
410 .open_file = fio_sgio_open,
411 .close_file = generic_close_file,
412 .flags = FIO_SYNCIO | FIO_RAWIO,
415 #else /* FIO_HAVE_SGIO */
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.
422 static int fio_sgio_init(struct thread_data fio_unused *td)
424 fprintf(stderr, "fio: sgio not available\n");
428 static struct ioengine_ops ioengine = {
430 .version = FIO_IOOPS_VERSION,
431 .init = fio_sgio_init,
436 static void fio_init fio_sgio_register(void)
438 register_ioengine(&ioengine);
441 static void fio_exit fio_sgio_unregister(void)
443 unregister_ioengine(&ioengine);