4 * IO engine that uses the Linux SG v3 interface to talk to SCSI devices
19 unsigned char cdb[10];
24 struct sgio_cmd *cmds;
33 static void sgio_hdr_init(struct sgio_data *sd, struct sg_io_hdr *hdr,
34 struct io_u *io_u, int fs)
36 struct sgio_cmd *sc = &sd->cmds[io_u->index];
38 memset(hdr, 0, sizeof(*hdr));
39 memset(sc->cdb, 0, sizeof(sc->cdb));
41 hdr->interface_id = 'S';
43 hdr->cmd_len = sizeof(sc->cdb);
44 hdr->pack_id = io_u->index;
48 hdr->dxferp = io_u->xfer_buf;
49 hdr->dxfer_len = io_u->xfer_buflen;
53 static int pollin_events(struct pollfd *pfds, int fds)
57 for (i = 0; i < fds; i++)
58 if (pfds[i].revents & POLLIN)
64 static int fio_sgio_getevents(struct thread_data *td, unsigned int min,
66 const struct timespec fio_unused *t)
68 struct sgio_data *sd = td->io_ops->data;
69 int left = max, ret, r = 0;
70 void *buf = sd->sgbuf;
71 unsigned int i, events;
75 * Fill in the file descriptors
77 for_each_file(td, f, i) {
79 * don't block for min events == 0
82 sd->fd_flags[i] = fio_set_fd_nonblocking(f->fd, "sg");
86 sd->pfds[i].fd = f->fd;
87 sd->pfds[i].events = POLLIN;
97 ret = poll(sd->pfds, td->o.nr_files, -1);
101 td_verror(td, errno, "poll");
106 if (pollin_events(sd->pfds, td->o.nr_files))
116 for_each_file(td, f, i) {
117 ret = read(f->fd, p, left * sizeof(struct sg_io_hdr));
122 td_verror(td, errno, "read");
126 events += ret / sizeof(struct sg_io_hdr);
140 for (i = 0; i < events; i++) {
141 struct sg_io_hdr *hdr = (struct sg_io_hdr *) buf + i;
143 sd->events[i] = hdr->usr_ptr;
148 for_each_file(td, f, i) {
149 if (sd->fd_flags[i] == -1)
152 if (fcntl(f->fd, F_SETFL, sd->fd_flags[i]) < 0)
153 log_err("fio: sg failed to restore fcntl flags: %s\n", strerror(errno));
160 static int fio_sgio_ioctl_doio(struct thread_data *td,
161 struct fio_file *f, struct io_u *io_u)
163 struct sgio_data *sd = td->io_ops->data;
164 struct sg_io_hdr *hdr = &io_u->hdr;
167 sd->events[0] = io_u;
169 ret = ioctl(f->fd, SG_IO, hdr);
173 return FIO_Q_COMPLETED;
176 static int fio_sgio_rw_doio(struct fio_file *f, struct io_u *io_u, int do_sync)
178 struct sg_io_hdr *hdr = &io_u->hdr;
181 ret = write(f->fd, hdr, sizeof(*hdr));
186 ret = read(f->fd, hdr, sizeof(*hdr));
189 return FIO_Q_COMPLETED;
195 static int fio_sgio_doio(struct thread_data *td, struct io_u *io_u, int do_sync)
197 struct fio_file *f = io_u->file;
199 if (f->filetype == FIO_TYPE_BD)
200 return fio_sgio_ioctl_doio(td, f, io_u);
202 return fio_sgio_rw_doio(f, io_u, do_sync);
205 static int fio_sgio_prep(struct thread_data *td, struct io_u *io_u)
207 struct sg_io_hdr *hdr = &io_u->hdr;
208 struct sgio_data *sd = td->io_ops->data;
211 if (io_u->xfer_buflen & (sd->bs - 1)) {
212 log_err("read/write not sector aligned\n");
216 if (io_u->ddir == DDIR_READ) {
217 sgio_hdr_init(sd, hdr, io_u, 1);
219 hdr->dxfer_direction = SG_DXFER_FROM_DEV;
221 } else if (io_u->ddir == DDIR_WRITE) {
222 sgio_hdr_init(sd, hdr, io_u, 1);
224 hdr->dxfer_direction = SG_DXFER_TO_DEV;
227 sgio_hdr_init(sd, hdr, io_u, 0);
229 hdr->dxfer_direction = SG_DXFER_NONE;
233 if (hdr->dxfer_direction != SG_DXFER_NONE) {
234 nr_blocks = io_u->xfer_buflen / sd->bs;
235 lba = io_u->offset / sd->bs;
236 hdr->cmdp[2] = (unsigned char) ((lba >> 24) & 0xff);
237 hdr->cmdp[3] = (unsigned char) ((lba >> 16) & 0xff);
238 hdr->cmdp[4] = (unsigned char) ((lba >> 8) & 0xff);
239 hdr->cmdp[5] = (unsigned char) (lba & 0xff);
240 hdr->cmdp[7] = (unsigned char) ((nr_blocks >> 8) & 0xff);
241 hdr->cmdp[8] = (unsigned char) (nr_blocks & 0xff);
247 static int fio_sgio_queue(struct thread_data *td, struct io_u *io_u)
249 struct sg_io_hdr *hdr = &io_u->hdr;
250 int ret, do_sync = 0;
252 fio_ro_check(td, io_u);
254 if (td->o.sync_io || td->o.odirect || ddir_sync(io_u->ddir))
257 ret = fio_sgio_doio(td, io_u, do_sync);
261 else if (hdr->status) {
262 io_u->resid = hdr->resid;
267 td_verror(td, io_u->error, "xfer");
268 return FIO_Q_COMPLETED;
274 static struct io_u *fio_sgio_event(struct thread_data *td, int event)
276 struct sgio_data *sd = td->io_ops->data;
278 return sd->events[event];
281 static int fio_sgio_get_bs(struct thread_data *td, unsigned int *bs)
283 struct sgio_data *sd = td->io_ops->data;
285 struct sg_io_hdr *hdr;
286 unsigned char buf[8];
289 memset(&io_u, 0, sizeof(io_u));
290 io_u.file = td->files[0];
293 sgio_hdr_init(sd, hdr, &io_u, 0);
294 memset(buf, 0, sizeof(buf));
297 hdr->dxfer_direction = SG_DXFER_FROM_DEV;
299 hdr->dxfer_len = sizeof(buf);
301 ret = fio_sgio_doio(td, &io_u, 1);
305 *bs = (buf[4] << 24) | (buf[5] << 16) | (buf[6] << 8) | buf[7];
309 static void fio_sgio_cleanup(struct thread_data *td)
311 struct sgio_data *sd = td->io_ops->data;
323 static int fio_sgio_init(struct thread_data *td)
325 struct sgio_data *sd;
327 sd = malloc(sizeof(*sd));
328 memset(sd, 0, sizeof(*sd));
329 sd->cmds = malloc(td->o.iodepth * sizeof(struct sgio_cmd));
330 memset(sd->cmds, 0, td->o.iodepth * sizeof(struct sgio_cmd));
331 sd->events = malloc(td->o.iodepth * sizeof(struct io_u *));
332 memset(sd->events, 0, td->o.iodepth * sizeof(struct io_u *));
333 sd->pfds = malloc(sizeof(struct pollfd) * td->o.nr_files);
334 memset(sd->pfds, 0, sizeof(struct pollfd) * td->o.nr_files);
335 sd->fd_flags = malloc(sizeof(int) * td->o.nr_files);
336 memset(sd->fd_flags, 0, sizeof(int) * td->o.nr_files);
337 sd->sgbuf = malloc(sizeof(struct sg_io_hdr) * td->o.iodepth);
338 memset(sd->sgbuf, 0, sizeof(struct sg_io_hdr) * td->o.iodepth);
340 td->io_ops->data = sd;
343 * we want to do it, regardless of whether odirect is set or not
345 td->o.override_sync = 1;
349 static int fio_sgio_type_check(struct thread_data *td, struct fio_file *f)
351 struct sgio_data *sd = td->io_ops->data;
354 if (f->filetype == FIO_TYPE_BD) {
355 if (ioctl(f->fd, BLKSSZGET, &bs) < 0) {
356 td_verror(td, errno, "ioctl");
359 } else if (f->filetype == FIO_TYPE_CHAR) {
362 if (ioctl(f->fd, SG_GET_VERSION_NUM, &version) < 0) {
363 td_verror(td, errno, "ioctl");
367 ret = fio_sgio_get_bs(td, &bs);
371 log_err("ioengine sg only works on block devices\n");
377 if (f->filetype == FIO_TYPE_BD) {
378 td->io_ops->getevents = NULL;
379 td->io_ops->event = NULL;
385 static int fio_sgio_open(struct thread_data *td, struct fio_file *f)
387 struct sgio_data *sd = td->io_ops->data;
390 ret = generic_open_file(td, f);
394 if (sd && !sd->type_checked && fio_sgio_type_check(td, f)) {
395 ret = generic_close_file(td, f);
402 static struct ioengine_ops ioengine = {
404 .version = FIO_IOOPS_VERSION,
405 .init = fio_sgio_init,
406 .prep = fio_sgio_prep,
407 .queue = fio_sgio_queue,
408 .getevents = fio_sgio_getevents,
409 .event = fio_sgio_event,
410 .cleanup = fio_sgio_cleanup,
411 .open_file = fio_sgio_open,
412 .close_file = generic_close_file,
413 .get_file_size = generic_get_file_size,
414 .flags = FIO_SYNCIO | FIO_RAWIO,
417 #else /* FIO_HAVE_SGIO */
420 * When we have a proper configure system in place, we simply wont build
421 * and install this io engine. For now install a crippled version that
422 * just complains and fails to load.
424 static int fio_sgio_init(struct thread_data fio_unused *td)
426 log_err("fio: ioengine sg not available\n");
430 static struct ioengine_ops ioengine = {
432 .version = FIO_IOOPS_VERSION,
433 .init = fio_sgio_init,
438 static void fio_init fio_sgio_register(void)
440 register_ioengine(&ioengine);
443 static void fio_exit fio_sgio_unregister(void)
445 unregister_ioengine(&ioengine);