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, int min, int max,
65 struct timespec fio_unused *t)
67 struct sgio_data *sd = td->io_ops->data;
68 int left = max, ret, r = 0;
69 void *buf = sd->sgbuf;
70 unsigned int i, events;
74 * Fill in the file descriptors
76 for_each_file(td, f, i) {
78 * don't block for min events == 0
81 sd->fd_flags[i] = fcntl(f->fd, F_GETFL);
82 fcntl(f->fd, F_SETFL, sd->fd_flags[i] | O_NONBLOCK);
84 sd->pfds[i].fd = f->fd;
85 sd->pfds[i].events = POLLIN;
95 ret = poll(sd->pfds, td->o.nr_files, -1);
99 td_verror(td, errno, "poll");
104 if (pollin_events(sd->pfds, td->o.nr_files))
114 for_each_file(td, f, i) {
115 ret = read(f->fd, p, left * sizeof(struct sg_io_hdr));
120 td_verror(td, errno, "read");
124 events += ret / sizeof(struct sg_io_hdr);
138 for (i = 0; i < events; i++) {
139 struct sg_io_hdr *hdr = (struct sg_io_hdr *) buf + i;
141 sd->events[i] = hdr->usr_ptr;
146 for_each_file(td, f, i)
147 fcntl(f->fd, F_SETFL, sd->fd_flags[i]);
153 static int fio_sgio_ioctl_doio(struct thread_data *td,
154 struct fio_file *f, struct io_u *io_u)
156 struct sgio_data *sd = td->io_ops->data;
157 struct sg_io_hdr *hdr = &io_u->hdr;
160 sd->events[0] = io_u;
162 ret = ioctl(f->fd, SG_IO, hdr);
166 return FIO_Q_COMPLETED;
169 static int fio_sgio_rw_doio(struct fio_file *f, struct io_u *io_u, int sync)
171 struct sg_io_hdr *hdr = &io_u->hdr;
174 ret = write(f->fd, hdr, sizeof(*hdr));
179 ret = read(f->fd, hdr, sizeof(*hdr));
182 return FIO_Q_COMPLETED;
188 static int fio_sgio_doio(struct thread_data *td, struct io_u *io_u, int sync)
190 struct fio_file *f = io_u->file;
192 if (f->filetype == FIO_TYPE_BD)
193 return fio_sgio_ioctl_doio(td, f, io_u);
195 return fio_sgio_rw_doio(f, io_u, sync);
198 static int fio_sgio_prep(struct thread_data *td, struct io_u *io_u)
200 struct sg_io_hdr *hdr = &io_u->hdr;
201 struct sgio_data *sd = td->io_ops->data;
204 if (io_u->xfer_buflen & (sd->bs - 1)) {
205 log_err("read/write not sector aligned\n");
209 if (io_u->ddir == DDIR_READ) {
210 sgio_hdr_init(sd, hdr, io_u, 1);
212 hdr->dxfer_direction = SG_DXFER_FROM_DEV;
214 } else if (io_u->ddir == DDIR_WRITE) {
215 sgio_hdr_init(sd, hdr, io_u, 1);
217 hdr->dxfer_direction = SG_DXFER_TO_DEV;
220 sgio_hdr_init(sd, hdr, io_u, 0);
222 hdr->dxfer_direction = SG_DXFER_NONE;
226 if (hdr->dxfer_direction != SG_DXFER_NONE) {
227 nr_blocks = io_u->xfer_buflen / sd->bs;
228 lba = io_u->offset / sd->bs;
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);
240 static int fio_sgio_queue(struct thread_data *td, struct io_u *io_u)
242 struct sg_io_hdr *hdr = &io_u->hdr;
245 ret = fio_sgio_doio(td, io_u, io_u->ddir == DDIR_SYNC);
249 else if (hdr->status) {
250 io_u->resid = hdr->resid;
255 td_verror(td, io_u->error, "xfer");
256 return FIO_Q_COMPLETED;
262 static struct io_u *fio_sgio_event(struct thread_data *td, int event)
264 struct sgio_data *sd = td->io_ops->data;
266 return sd->events[event];
269 static int fio_sgio_get_bs(struct thread_data *td, unsigned int *bs)
271 struct sgio_data *sd = td->io_ops->data;
273 struct sg_io_hdr *hdr;
274 unsigned char buf[8];
277 io_u = __get_io_u(td);
278 io_u->file = &td->files[0];
282 sgio_hdr_init(sd, hdr, io_u, 0);
283 memset(buf, 0, sizeof(buf));
286 hdr->dxfer_direction = SG_DXFER_FROM_DEV;
288 hdr->dxfer_len = sizeof(buf);
290 ret = fio_sgio_doio(td, io_u, 1);
296 *bs = (buf[4] << 24) | (buf[5] << 16) | (buf[6] << 8) | buf[7];
301 static void fio_sgio_cleanup(struct thread_data *td)
303 struct sgio_data *sd = td->io_ops->data;
313 td->io_ops->data = NULL;
317 static int fio_sgio_init(struct thread_data *td)
319 struct sgio_data *sd;
321 sd = malloc(sizeof(*sd));
322 memset(sd, 0, sizeof(*sd));
323 sd->cmds = malloc(td->o.iodepth * sizeof(struct sgio_cmd));
324 memset(sd->cmds, 0, td->o.iodepth * sizeof(struct sgio_cmd));
325 sd->events = malloc(td->o.iodepth * sizeof(struct io_u *));
326 memset(sd->events, 0, td->o.iodepth * sizeof(struct io_u *));
327 sd->pfds = malloc(sizeof(struct pollfd) * td->o.nr_files);
328 memset(sd->pfds, 0, sizeof(struct pollfd) * td->o.nr_files);
329 sd->fd_flags = malloc(sizeof(int) * td->o.nr_files);
330 memset(sd->fd_flags, 0, sizeof(int) * td->o.nr_files);
331 sd->sgbuf = malloc(sizeof(struct sg_io_hdr) * td->o.iodepth);
332 memset(sd->sgbuf, 0, sizeof(struct sg_io_hdr) * td->o.iodepth);
334 td->io_ops->data = sd;
337 * we want to do it, regardless of whether odirect is set or not
339 td->o.override_sync = 1;
343 static int fio_sgio_type_check(struct thread_data *td, struct fio_file *f)
345 struct sgio_data *sd = td->io_ops->data;
348 if (f->filetype == FIO_TYPE_BD) {
349 if (ioctl(f->fd, BLKSSZGET, &bs) < 0) {
350 td_verror(td, errno, "ioctl");
353 } else if (f->filetype == FIO_TYPE_CHAR) {
356 if (ioctl(f->fd, SG_GET_VERSION_NUM, &version) < 0) {
357 td_verror(td, errno, "ioctl");
361 ret = fio_sgio_get_bs(td, &bs);
365 log_err("ioengine sgio only works on block devices\n");
371 if (f->filetype == FIO_TYPE_BD) {
372 td->io_ops->getevents = NULL;
373 td->io_ops->event = NULL;
379 static int fio_sgio_open(struct thread_data *td, struct fio_file *f)
381 struct sgio_data *sd = td->io_ops->data;
384 ret = generic_open_file(td, f);
388 if (sd && !sd->type_checked && fio_sgio_type_check(td, f)) {
389 generic_close_file(td, f);
396 static struct ioengine_ops ioengine = {
398 .version = FIO_IOOPS_VERSION,
399 .init = fio_sgio_init,
400 .prep = fio_sgio_prep,
401 .queue = fio_sgio_queue,
402 .getevents = fio_sgio_getevents,
403 .event = fio_sgio_event,
404 .cleanup = fio_sgio_cleanup,
405 .open_file = fio_sgio_open,
406 .close_file = generic_close_file,
407 .flags = FIO_SYNCIO | FIO_RAWIO,
410 #else /* FIO_HAVE_SGIO */
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.
417 static int fio_sgio_init(struct thread_data fio_unused *td)
419 fprintf(stderr, "fio: sgio not available\n");
423 static struct ioengine_ops ioengine = {
425 .version = FIO_IOOPS_VERSION,
426 .init = fio_sgio_init,
431 static void fio_init fio_sgio_register(void)
433 register_ioengine(&ioengine);
436 static void fio_exit fio_sgio_unregister(void)
438 unregister_ioengine(&ioengine);