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)
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] = fcntl(f->fd, F_GETFL);
83 fcntl(f->fd, F_SETFL, sd->fd_flags[i] | O_NONBLOCK);
85 sd->pfds[i].fd = f->fd;
86 sd->pfds[i].events = POLLIN;
96 ret = poll(sd->pfds, td->o.nr_files, -1);
100 td_verror(td, errno, "poll");
105 if (pollin_events(sd->pfds, td->o.nr_files))
115 for_each_file(td, f, i) {
116 ret = read(f->fd, p, left * sizeof(struct sg_io_hdr));
121 td_verror(td, errno, "read");
125 events += ret / sizeof(struct sg_io_hdr);
139 for (i = 0; i < events; i++) {
140 struct sg_io_hdr *hdr = (struct sg_io_hdr *) buf + i;
142 sd->events[i] = hdr->usr_ptr;
147 for_each_file(td, f, i)
148 fcntl(f->fd, F_SETFL, sd->fd_flags[i]);
154 static int fio_sgio_ioctl_doio(struct thread_data *td,
155 struct fio_file *f, struct io_u *io_u)
157 struct sgio_data *sd = td->io_ops->data;
158 struct sg_io_hdr *hdr = &io_u->hdr;
161 sd->events[0] = io_u;
163 ret = ioctl(f->fd, SG_IO, hdr);
167 return FIO_Q_COMPLETED;
170 static int fio_sgio_rw_doio(struct fio_file *f, struct io_u *io_u, int sync)
172 struct sg_io_hdr *hdr = &io_u->hdr;
175 ret = write(f->fd, hdr, sizeof(*hdr));
180 ret = read(f->fd, hdr, sizeof(*hdr));
183 return FIO_Q_COMPLETED;
189 static int fio_sgio_doio(struct thread_data *td, struct io_u *io_u, int sync)
191 struct fio_file *f = io_u->file;
193 if (f->filetype == FIO_TYPE_BD)
194 return fio_sgio_ioctl_doio(td, f, io_u);
196 return fio_sgio_rw_doio(f, io_u, sync);
199 static int fio_sgio_prep(struct thread_data *td, struct io_u *io_u)
201 struct sg_io_hdr *hdr = &io_u->hdr;
202 struct sgio_data *sd = td->io_ops->data;
205 if (io_u->xfer_buflen & (sd->bs - 1)) {
206 log_err("read/write not sector aligned\n");
210 if (io_u->ddir == DDIR_READ) {
211 sgio_hdr_init(sd, hdr, io_u, 1);
213 hdr->dxfer_direction = SG_DXFER_FROM_DEV;
215 } else if (io_u->ddir == DDIR_WRITE) {
216 sgio_hdr_init(sd, hdr, io_u, 1);
218 hdr->dxfer_direction = SG_DXFER_TO_DEV;
221 sgio_hdr_init(sd, hdr, io_u, 0);
223 hdr->dxfer_direction = SG_DXFER_NONE;
227 if (hdr->dxfer_direction != SG_DXFER_NONE) {
228 nr_blocks = io_u->xfer_buflen / sd->bs;
229 lba = io_u->offset / sd->bs;
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);
241 static int fio_sgio_queue(struct thread_data *td, struct io_u *io_u)
243 struct sg_io_hdr *hdr = &io_u->hdr;
246 ret = fio_sgio_doio(td, io_u, io_u->ddir == DDIR_SYNC);
250 else if (hdr->status) {
251 io_u->resid = hdr->resid;
256 td_verror(td, io_u->error, "xfer");
257 return FIO_Q_COMPLETED;
263 static struct io_u *fio_sgio_event(struct thread_data *td, int event)
265 struct sgio_data *sd = td->io_ops->data;
267 return sd->events[event];
270 static int fio_sgio_get_bs(struct thread_data *td, unsigned int *bs)
272 struct sgio_data *sd = td->io_ops->data;
274 struct sg_io_hdr *hdr;
275 unsigned char buf[8];
278 io_u = __get_io_u(td);
279 io_u->file = &td->files[0];
283 sgio_hdr_init(sd, hdr, io_u, 0);
284 memset(buf, 0, sizeof(buf));
287 hdr->dxfer_direction = SG_DXFER_FROM_DEV;
289 hdr->dxfer_len = sizeof(buf);
291 ret = fio_sgio_doio(td, io_u, 1);
297 *bs = (buf[4] << 24) | (buf[5] << 16) | (buf[6] << 8) | buf[7];
302 static void fio_sgio_cleanup(struct thread_data *td)
304 struct sgio_data *sd = td->io_ops->data;
314 td->io_ops->data = NULL;
318 static int fio_sgio_init(struct thread_data *td)
320 struct sgio_data *sd;
322 sd = malloc(sizeof(*sd));
323 memset(sd, 0, sizeof(*sd));
324 sd->cmds = malloc(td->o.iodepth * sizeof(struct sgio_cmd));
325 memset(sd->cmds, 0, td->o.iodepth * sizeof(struct sgio_cmd));
326 sd->events = malloc(td->o.iodepth * sizeof(struct io_u *));
327 memset(sd->events, 0, td->o.iodepth * sizeof(struct io_u *));
328 sd->pfds = malloc(sizeof(struct pollfd) * td->o.nr_files);
329 memset(sd->pfds, 0, sizeof(struct pollfd) * td->o.nr_files);
330 sd->fd_flags = malloc(sizeof(int) * td->o.nr_files);
331 memset(sd->fd_flags, 0, sizeof(int) * td->o.nr_files);
332 sd->sgbuf = malloc(sizeof(struct sg_io_hdr) * td->o.iodepth);
333 memset(sd->sgbuf, 0, sizeof(struct sg_io_hdr) * td->o.iodepth);
335 td->io_ops->data = sd;
338 * we want to do it, regardless of whether odirect is set or not
340 td->o.override_sync = 1;
344 static int fio_sgio_type_check(struct thread_data *td, struct fio_file *f)
346 struct sgio_data *sd = td->io_ops->data;
349 if (f->filetype == FIO_TYPE_BD) {
350 if (ioctl(f->fd, BLKSSZGET, &bs) < 0) {
351 td_verror(td, errno, "ioctl");
354 } else if (f->filetype == FIO_TYPE_CHAR) {
357 if (ioctl(f->fd, SG_GET_VERSION_NUM, &version) < 0) {
358 td_verror(td, errno, "ioctl");
362 ret = fio_sgio_get_bs(td, &bs);
366 log_err("ioengine sgio only works on block devices\n");
372 if (f->filetype == FIO_TYPE_BD) {
373 td->io_ops->getevents = NULL;
374 td->io_ops->event = NULL;
380 static int fio_sgio_open(struct thread_data *td, struct fio_file *f)
382 struct sgio_data *sd = td->io_ops->data;
385 ret = generic_open_file(td, f);
389 if (sd && !sd->type_checked && fio_sgio_type_check(td, f)) {
390 generic_close_file(td, f);
397 static struct ioengine_ops ioengine = {
399 .version = FIO_IOOPS_VERSION,
400 .init = fio_sgio_init,
401 .prep = fio_sgio_prep,
402 .queue = fio_sgio_queue,
403 .getevents = fio_sgio_getevents,
404 .event = fio_sgio_event,
405 .cleanup = fio_sgio_cleanup,
406 .open_file = fio_sgio_open,
407 .close_file = generic_close_file,
408 .flags = FIO_SYNCIO | FIO_RAWIO,
411 #else /* FIO_HAVE_SGIO */
414 * When we have a proper configure system in place, we simply wont build
415 * and install this io engine. For now install a crippled version that
416 * just complains and fails to load.
418 static int fio_sgio_init(struct thread_data fio_unused *td)
420 fprintf(stderr, "fio: sgio not available\n");
424 static struct ioengine_ops ioengine = {
426 .version = FIO_IOOPS_VERSION,
427 .init = fio_sgio_init,
432 static void fio_init fio_sgio_register(void)
434 register_ioengine(&ioengine);
437 static void fio_exit fio_sgio_unregister(void)
439 unregister_ioengine(&ioengine);