exp: fix issues around int vs size_t
[fio.git] / engines / sg.c
CommitLineData
2866c82d 1/*
da751ca9
JA
2 * sg engine
3 *
4 * IO engine that uses the Linux SG v3 interface to talk to SCSI devices
2866c82d
JA
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>
5f350952
JA
13
14#include "../fio.h"
2866c82d 15
34cfcdaf
JA
16#ifdef FIO_HAVE_SGIO
17
2866c82d
JA
18struct sgio_cmd {
19 unsigned char cdb[10];
20 int nr;
21};
22
23struct sgio_data {
24 struct sgio_cmd *cmds;
25 struct io_u **events;
dc0deca2
JA
26 struct pollfd *pfds;
27 int *fd_flags;
28 void *sgbuf;
2866c82d 29 unsigned int bs;
b5af8293 30 int type_checked;
2866c82d
JA
31};
32
33static void sgio_hdr_init(struct sgio_data *sd, struct sg_io_hdr *hdr,
34 struct io_u *io_u, int fs)
35{
36 struct sgio_cmd *sc = &sd->cmds[io_u->index];
37
38 memset(hdr, 0, sizeof(*hdr));
39 memset(sc->cdb, 0, sizeof(sc->cdb));
40
41 hdr->interface_id = 'S';
42 hdr->cmdp = sc->cdb;
43 hdr->cmd_len = sizeof(sc->cdb);
44 hdr->pack_id = io_u->index;
45 hdr->usr_ptr = io_u;
46
47 if (fs) {
cec6b55d
JA
48 hdr->dxferp = io_u->xfer_buf;
49 hdr->dxfer_len = io_u->xfer_buflen;
2866c82d
JA
50 }
51}
52
adee86c5
JA
53static int pollin_events(struct pollfd *pfds, int fds)
54{
55 int i;
56
57 for (i = 0; i < fds; i++)
58 if (pfds[i].revents & POLLIN)
59 return 1;
60
61 return 0;
62}
2866c82d 63
e7d2e616 64static int fio_sgio_getevents(struct thread_data *td, unsigned int min,
1f440ece
JA
65 unsigned int max,
66 const struct timespec fio_unused *t)
2866c82d
JA
67{
68 struct sgio_data *sd = td->io_ops->data;
af52b345 69 int left = max, ret, r = 0;
dc0deca2 70 void *buf = sd->sgbuf;
af52b345 71 unsigned int i, events;
946ff865 72 struct fio_file *f;
2866c82d
JA
73
74 /*
adee86c5 75 * Fill in the file descriptors
2866c82d 76 */
adee86c5
JA
77 for_each_file(td, f, i) {
78 /*
79 * don't block for min events == 0
80 */
4a851614 81 if (!min)
3a35845f
JA
82 sd->fd_flags[i] = fio_set_fd_nonblocking(f->fd, "sg");
83 else
84 sd->fd_flags[i] = -1;
4a851614 85
dc0deca2
JA
86 sd->pfds[i].fd = f->fd;
87 sd->pfds[i].events = POLLIN;
2866c82d
JA
88 }
89
90 while (left) {
adee86c5
JA
91 void *p;
92
2866c82d
JA
93 do {
94 if (!min)
95 break;
adee86c5 96
2dc1bbeb 97 ret = poll(sd->pfds, td->o.nr_files, -1);
adee86c5 98 if (ret < 0) {
adee86c5 99 if (!r)
22819ec2 100 r = -errno;
e1161c32 101 td_verror(td, errno, "poll");
adee86c5
JA
102 break;
103 } else if (!ret)
104 continue;
105
2dc1bbeb 106 if (pollin_events(sd->pfds, td->o.nr_files))
2866c82d
JA
107 break;
108 } while (1);
109
adee86c5 110 if (r < 0)
2866c82d 111 break;
adee86c5
JA
112
113re_read:
114 p = buf;
115 events = 0;
116 for_each_file(td, f, i) {
117 ret = read(f->fd, p, left * sizeof(struct sg_io_hdr));
118 if (ret < 0) {
119 if (errno == EAGAIN)
120 continue;
22819ec2 121 r = -errno;
e1161c32 122 td_verror(td, errno, "read");
adee86c5
JA
123 break;
124 } else if (ret) {
125 p += ret;
126 events += ret / sizeof(struct sg_io_hdr);
127 }
128 }
129
130 if (r < 0)
2866c82d 131 break;
adee86c5
JA
132 if (!events) {
133 usleep(1000);
134 goto re_read;
135 }
2866c82d 136
2866c82d
JA
137 left -= events;
138 r += events;
139
140 for (i = 0; i < events; i++) {
141 struct sg_io_hdr *hdr = (struct sg_io_hdr *) buf + i;
142
143 sd->events[i] = hdr->usr_ptr;
144 }
145 }
146
adee86c5 147 if (!min) {
affe05a9 148 for_each_file(td, f, i) {
3a35845f
JA
149 if (sd->fd_flags[i] == -1)
150 continue;
151
affe05a9
JA
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));
154 }
adee86c5 155 }
2866c82d 156
2866c82d
JA
157 return r;
158}
159
7a16dd02
JA
160static int fio_sgio_ioctl_doio(struct thread_data *td,
161 struct fio_file *f, struct io_u *io_u)
2866c82d
JA
162{
163 struct sgio_data *sd = td->io_ops->data;
164 struct sg_io_hdr *hdr = &io_u->hdr;
36167d82 165 int ret;
2866c82d
JA
166
167 sd->events[0] = io_u;
168
36167d82
JA
169 ret = ioctl(f->fd, SG_IO, hdr);
170 if (ret < 0)
a05bd42d 171 return ret;
36167d82
JA
172
173 return FIO_Q_COMPLETED;
2866c82d
JA
174}
175
2b13e716 176static int fio_sgio_rw_doio(struct fio_file *f, struct io_u *io_u, int do_sync)
2866c82d
JA
177{
178 struct sg_io_hdr *hdr = &io_u->hdr;
179 int ret;
180
53cdc686 181 ret = write(f->fd, hdr, sizeof(*hdr));
2866c82d 182 if (ret < 0)
a05bd42d 183 return ret;
2866c82d 184
2b13e716 185 if (do_sync) {
53cdc686 186 ret = read(f->fd, hdr, sizeof(*hdr));
2866c82d 187 if (ret < 0)
a05bd42d 188 return ret;
36167d82 189 return FIO_Q_COMPLETED;
2866c82d
JA
190 }
191
36167d82 192 return FIO_Q_QUEUED;
2866c82d
JA
193}
194
2b13e716 195static int fio_sgio_doio(struct thread_data *td, struct io_u *io_u, int do_sync)
2866c82d 196{
53cdc686
JA
197 struct fio_file *f = io_u->file;
198
af52b345 199 if (f->filetype == FIO_TYPE_BD)
53cdc686 200 return fio_sgio_ioctl_doio(td, f, io_u);
2866c82d 201
2b13e716 202 return fio_sgio_rw_doio(f, io_u, do_sync);
2866c82d
JA
203}
204
2866c82d
JA
205static int fio_sgio_prep(struct thread_data *td, struct io_u *io_u)
206{
207 struct sg_io_hdr *hdr = &io_u->hdr;
208 struct sgio_data *sd = td->io_ops->data;
209 int nr_blocks, lba;
210
cec6b55d 211 if (io_u->xfer_buflen & (sd->bs - 1)) {
2866c82d
JA
212 log_err("read/write not sector aligned\n");
213 return EINVAL;
214 }
215
2866c82d 216 if (io_u->ddir == DDIR_READ) {
87dc1ab1
JA
217 sgio_hdr_init(sd, hdr, io_u, 1);
218
2866c82d
JA
219 hdr->dxfer_direction = SG_DXFER_FROM_DEV;
220 hdr->cmdp[0] = 0x28;
87dc1ab1
JA
221 } else if (io_u->ddir == DDIR_WRITE) {
222 sgio_hdr_init(sd, hdr, io_u, 1);
223
2866c82d
JA
224 hdr->dxfer_direction = SG_DXFER_TO_DEV;
225 hdr->cmdp[0] = 0x2a;
87dc1ab1
JA
226 } else {
227 sgio_hdr_init(sd, hdr, io_u, 0);
228
229 hdr->dxfer_direction = SG_DXFER_NONE;
230 hdr->cmdp[0] = 0x35;
231 }
232
233 if (hdr->dxfer_direction != SG_DXFER_NONE) {
cec6b55d 234 nr_blocks = io_u->xfer_buflen / sd->bs;
87dc1ab1 235 lba = io_u->offset / sd->bs;
1e97cce9
JA
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);
2866c82d
JA
242 }
243
2866c82d
JA
244 return 0;
245}
246
247static int fio_sgio_queue(struct thread_data *td, struct io_u *io_u)
248{
249 struct sg_io_hdr *hdr = &io_u->hdr;
f6db4fa5 250 int ret, do_sync = 0;
2866c82d 251
7101d9c2
JA
252 fio_ro_check(td, io_u);
253
f6db4fa5
JA
254 if (td->o.sync_io || td->o.odirect || ddir_sync(io_u->ddir))
255 do_sync = 1;
256
257 ret = fio_sgio_doio(td, io_u, do_sync);
2866c82d
JA
258
259 if (ret < 0)
260 io_u->error = errno;
261 else if (hdr->status) {
262 io_u->resid = hdr->resid;
263 io_u->error = EIO;
264 }
265
95bcd815 266 if (io_u->error) {
e1161c32 267 td_verror(td, io_u->error, "xfer");
36167d82 268 return FIO_Q_COMPLETED;
95bcd815
JA
269 }
270
36167d82 271 return ret;
2866c82d
JA
272}
273
274static struct io_u *fio_sgio_event(struct thread_data *td, int event)
275{
276 struct sgio_data *sd = td->io_ops->data;
277
278 return sd->events[event];
279}
280
281static int fio_sgio_get_bs(struct thread_data *td, unsigned int *bs)
282{
283 struct sgio_data *sd = td->io_ops->data;
2039ab1d 284 struct io_u io_u;
2866c82d
JA
285 struct sg_io_hdr *hdr;
286 unsigned char buf[8];
287 int ret;
288
2039ab1d
JA
289 memset(&io_u, 0, sizeof(io_u));
290 io_u.file = td->files[0];
2866c82d 291
2039ab1d
JA
292 hdr = &io_u.hdr;
293 sgio_hdr_init(sd, hdr, &io_u, 0);
2866c82d
JA
294 memset(buf, 0, sizeof(buf));
295
296 hdr->cmdp[0] = 0x25;
297 hdr->dxfer_direction = SG_DXFER_FROM_DEV;
298 hdr->dxferp = buf;
299 hdr->dxfer_len = sizeof(buf);
300
2039ab1d
JA
301 ret = fio_sgio_doio(td, &io_u, 1);
302 if (ret)
2866c82d 303 return ret;
2866c82d
JA
304
305 *bs = (buf[4] << 24) | (buf[5] << 16) | (buf[6] << 8) | buf[7];
2866c82d
JA
306 return 0;
307}
308
309static void fio_sgio_cleanup(struct thread_data *td)
310{
dc0deca2
JA
311 struct sgio_data *sd = td->io_ops->data;
312
313 if (sd) {
314 free(sd->events);
315 free(sd->cmds);
316 free(sd->fd_flags);
317 free(sd->pfds);
318 free(sd->sgbuf);
319 free(sd);
2866c82d
JA
320 }
321}
322
323static int fio_sgio_init(struct thread_data *td)
324{
325 struct sgio_data *sd;
2866c82d
JA
326
327 sd = malloc(sizeof(*sd));
cb781c75 328 memset(sd, 0, sizeof(*sd));
2dc1bbeb
JA
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);
dc0deca2 339
2866c82d
JA
340 td->io_ops->data = sd;
341
b5af8293
JA
342 /*
343 * we want to do it, regardless of whether odirect is set or not
344 */
2dc1bbeb 345 td->o.override_sync = 1;
b5af8293
JA
346 return 0;
347}
348
349static int fio_sgio_type_check(struct thread_data *td, struct fio_file *f)
350{
351 struct sgio_data *sd = td->io_ops->data;
352 unsigned int bs;
353
af52b345 354 if (f->filetype == FIO_TYPE_BD) {
53cdc686 355 if (ioctl(f->fd, BLKSSZGET, &bs) < 0) {
e1161c32 356 td_verror(td, errno, "ioctl");
b5af8293 357 return 1;
2866c82d 358 }
af52b345 359 } else if (f->filetype == FIO_TYPE_CHAR) {
b5af8293 360 int version, ret;
2866c82d 361
53cdc686 362 if (ioctl(f->fd, SG_GET_VERSION_NUM, &version) < 0) {
e1161c32 363 td_verror(td, errno, "ioctl");
b5af8293 364 return 1;
2866c82d
JA
365 }
366
367 ret = fio_sgio_get_bs(td, &bs);
368 if (ret)
b5af8293 369 return 1;
2866c82d 370 } else {
d0c70934 371 log_err("ioengine sg only works on block devices\n");
b5af8293 372 return 1;
2866c82d
JA
373 }
374
375 sd->bs = bs;
376
af52b345 377 if (f->filetype == FIO_TYPE_BD) {
36167d82
JA
378 td->io_ops->getevents = NULL;
379 td->io_ops->event = NULL;
380 }
2866c82d 381
2866c82d 382 return 0;
b5af8293
JA
383}
384
385static int fio_sgio_open(struct thread_data *td, struct fio_file *f)
386{
387 struct sgio_data *sd = td->io_ops->data;
388 int ret;
389
390 ret = generic_open_file(td, f);
391 if (ret)
392 return ret;
393
15ba640a 394 if (sd && !sd->type_checked && fio_sgio_type_check(td, f)) {
6977bcd0 395 ret = generic_close_file(td, f);
b5af8293
JA
396 return 1;
397 }
398
399 return 0;
2866c82d
JA
400}
401
5f350952 402static struct ioengine_ops ioengine = {
2866c82d
JA
403 .name = "sg",
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,
b5af8293
JA
411 .open_file = fio_sgio_open,
412 .close_file = generic_close_file,
df9c26b1 413 .get_file_size = generic_get_file_size,
b2a15192 414 .flags = FIO_SYNCIO | FIO_RAWIO,
2866c82d 415};
34cfcdaf
JA
416
417#else /* FIO_HAVE_SGIO */
418
419/*
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.
423 */
424static int fio_sgio_init(struct thread_data fio_unused *td)
425{
a3edaf76 426 log_err("fio: ioengine sg not available\n");
34cfcdaf
JA
427 return 1;
428}
429
5f350952 430static struct ioengine_ops ioengine = {
d0c70934 431 .name = "sg",
34cfcdaf
JA
432 .version = FIO_IOOPS_VERSION,
433 .init = fio_sgio_init,
434};
435
436#endif
5f350952
JA
437
438static void fio_init fio_sgio_register(void)
439{
440 register_ioengine(&ioengine);
441}
442
443static void fio_exit fio_sgio_unregister(void)
444{
445 unregister_ioengine(&ioengine);
446}