[PATCH] SG io engine: better handling of multiple files
[fio.git] / engines / sg.c
CommitLineData
2866c82d
JA
1/*
2 * scsi generic sg v3 io engine
3 *
4 */
5#include <stdio.h>
6#include <stdlib.h>
7#include <unistd.h>
8#include <errno.h>
9#include <assert.h>
10#include <sys/poll.h>
5f350952
JA
11
12#include "../fio.h"
13#include "../os.h"
2866c82d 14
34cfcdaf
JA
15#ifdef FIO_HAVE_SGIO
16
2866c82d
JA
17struct sgio_cmd {
18 unsigned char cdb[10];
19 int nr;
20};
21
22struct sgio_data {
23 struct sgio_cmd *cmds;
24 struct io_u **events;
25 unsigned int bs;
26};
27
28static void sgio_hdr_init(struct sgio_data *sd, struct sg_io_hdr *hdr,
29 struct io_u *io_u, int fs)
30{
31 struct sgio_cmd *sc = &sd->cmds[io_u->index];
32
33 memset(hdr, 0, sizeof(*hdr));
34 memset(sc->cdb, 0, sizeof(sc->cdb));
35
36 hdr->interface_id = 'S';
37 hdr->cmdp = sc->cdb;
38 hdr->cmd_len = sizeof(sc->cdb);
39 hdr->pack_id = io_u->index;
40 hdr->usr_ptr = io_u;
41
42 if (fs) {
cec6b55d
JA
43 hdr->dxferp = io_u->xfer_buf;
44 hdr->dxfer_len = io_u->xfer_buflen;
2866c82d
JA
45 }
46}
47
48static int fio_sgio_ioctl_getevents(struct thread_data *td, int fio_unused min,
49 int max, struct timespec fio_unused *t)
50{
51 assert(max <= 1);
52
53 /*
54 * we can only have one finished io_u for sync io, since the depth
55 * is always 1
56 */
57 if (list_empty(&td->io_u_busylist))
58 return 0;
59
60 return 1;
61}
62
adee86c5
JA
63static int pollin_events(struct pollfd *pfds, int fds)
64{
65 int i;
66
67 for (i = 0; i < fds; i++)
68 if (pfds[i].revents & POLLIN)
69 return 1;
70
71 return 0;
72}
2866c82d
JA
73
74static int fio_sgio_getevents(struct thread_data *td, int min, int max,
75 struct timespec fio_unused *t)
76{
adee86c5
JA
77 /*
78 * normally hard coding &td->files[0] is a bug that needs to be fixed,
79 * but it's ok here as all files should point to the same device.
80 */
53cdc686 81 struct fio_file *f = &td->files[0];
2866c82d 82 struct sgio_data *sd = td->io_ops->data;
adee86c5
JA
83 int left = max, ret, events, i, r = 0, *fl;
84 struct pollfd *pfds;
85 void *buf;
2866c82d
JA
86
87 /*
adee86c5 88 * Fill in the file descriptors
2866c82d 89 */
adee86c5
JA
90 pfds = malloc(sizeof(struct pollfd) * td->nr_files);
91 fl = malloc(sizeof(int) * td->nr_files);
92
93 for_each_file(td, f, i) {
94 /*
95 * don't block for min events == 0
96 */
97 if (!min) {
98 fl[i] = fcntl(f->fd, F_GETFL);
99 fcntl(f->fd, F_SETFL, fl[i] | O_NONBLOCK);
100 }
101 pfds[i].fd = f->fd;
102 pfds[i].events = POLLIN;
2866c82d
JA
103 }
104
adee86c5 105 buf = malloc(max * sizeof(struct sg_io_hdr));
2866c82d 106 while (left) {
adee86c5
JA
107 void *p;
108
2866c82d
JA
109 do {
110 if (!min)
111 break;
adee86c5
JA
112
113 ret = poll(pfds, td->nr_files, -1);
114 if (ret < 0) {
115 td_verror(td, errno);
116 if (!r)
117 r = -1;
118 break;
119 } else if (!ret)
120 continue;
121
122 if (pollin_events(pfds, td->nr_files))
2866c82d
JA
123 break;
124 } while (1);
125
adee86c5 126 if (r < 0)
2866c82d 127 break;
adee86c5
JA
128
129re_read:
130 p = buf;
131 events = 0;
132 for_each_file(td, f, i) {
133 ret = read(f->fd, p, left * sizeof(struct sg_io_hdr));
134 if (ret < 0) {
135 if (errno == EAGAIN)
136 continue;
137 td_verror(td, errno);
138 r = -1;
139 break;
140 } else if (ret) {
141 p += ret;
142 events += ret / sizeof(struct sg_io_hdr);
143 }
144 }
145
146 if (r < 0)
2866c82d 147 break;
adee86c5
JA
148 if (!events) {
149 usleep(1000);
150 goto re_read;
151 }
2866c82d 152
2866c82d
JA
153 left -= events;
154 r += events;
155
156 for (i = 0; i < events; i++) {
157 struct sg_io_hdr *hdr = (struct sg_io_hdr *) buf + i;
158
159 sd->events[i] = hdr->usr_ptr;
160 }
161 }
162
adee86c5
JA
163 if (!min) {
164 for_each_file(td, f, i)
165 fcntl(f->fd, F_SETFL, fl[i]);
166 }
2866c82d
JA
167
168 free(buf);
adee86c5
JA
169 free(pfds);
170 free(fl);
2866c82d
JA
171 return r;
172}
173
7a16dd02
JA
174static int fio_sgio_ioctl_doio(struct thread_data *td,
175 struct fio_file *f, struct io_u *io_u)
2866c82d
JA
176{
177 struct sgio_data *sd = td->io_ops->data;
178 struct sg_io_hdr *hdr = &io_u->hdr;
179
180 sd->events[0] = io_u;
181
53cdc686 182 return ioctl(f->fd, SG_IO, hdr);
2866c82d
JA
183}
184
7a16dd02 185static int fio_sgio_rw_doio(struct fio_file *f, struct io_u *io_u, int sync)
2866c82d
JA
186{
187 struct sg_io_hdr *hdr = &io_u->hdr;
188 int ret;
189
53cdc686 190 ret = write(f->fd, hdr, sizeof(*hdr));
2866c82d
JA
191 if (ret < 0)
192 return errno;
193
194 if (sync) {
53cdc686 195 ret = read(f->fd, hdr, sizeof(*hdr));
2866c82d
JA
196 if (ret < 0)
197 return errno;
198 }
199
200 return 0;
201}
202
203static int fio_sgio_doio(struct thread_data *td, struct io_u *io_u, int sync)
204{
53cdc686
JA
205 struct fio_file *f = io_u->file;
206
2866c82d 207 if (td->filetype == FIO_TYPE_BD)
53cdc686 208 return fio_sgio_ioctl_doio(td, f, io_u);
2866c82d 209
7a16dd02 210 return fio_sgio_rw_doio(f, io_u, sync);
2866c82d
JA
211}
212
2866c82d
JA
213static int fio_sgio_prep(struct thread_data *td, struct io_u *io_u)
214{
215 struct sg_io_hdr *hdr = &io_u->hdr;
216 struct sgio_data *sd = td->io_ops->data;
217 int nr_blocks, lba;
218
cec6b55d 219 if (io_u->xfer_buflen & (sd->bs - 1)) {
2866c82d
JA
220 log_err("read/write not sector aligned\n");
221 return EINVAL;
222 }
223
2866c82d 224 if (io_u->ddir == DDIR_READ) {
87dc1ab1
JA
225 sgio_hdr_init(sd, hdr, io_u, 1);
226
2866c82d
JA
227 hdr->dxfer_direction = SG_DXFER_FROM_DEV;
228 hdr->cmdp[0] = 0x28;
87dc1ab1
JA
229 } else if (io_u->ddir == DDIR_WRITE) {
230 sgio_hdr_init(sd, hdr, io_u, 1);
231
2866c82d
JA
232 hdr->dxfer_direction = SG_DXFER_TO_DEV;
233 hdr->cmdp[0] = 0x2a;
87dc1ab1
JA
234 } else {
235 sgio_hdr_init(sd, hdr, io_u, 0);
236
237 hdr->dxfer_direction = SG_DXFER_NONE;
238 hdr->cmdp[0] = 0x35;
239 }
240
241 if (hdr->dxfer_direction != SG_DXFER_NONE) {
cec6b55d 242 nr_blocks = io_u->xfer_buflen / sd->bs;
87dc1ab1 243 lba = io_u->offset / sd->bs;
1e97cce9
JA
244 hdr->cmdp[2] = (unsigned char) ((lba >> 24) & 0xff);
245 hdr->cmdp[3] = (unsigned char) ((lba >> 16) & 0xff);
246 hdr->cmdp[4] = (unsigned char) ((lba >> 8) & 0xff);
247 hdr->cmdp[5] = (unsigned char) (lba & 0xff);
248 hdr->cmdp[7] = (unsigned char) ((nr_blocks >> 8) & 0xff);
249 hdr->cmdp[8] = (unsigned char) (nr_blocks & 0xff);
2866c82d
JA
250 }
251
2866c82d
JA
252 return 0;
253}
254
255static int fio_sgio_queue(struct thread_data *td, struct io_u *io_u)
256{
257 struct sg_io_hdr *hdr = &io_u->hdr;
258 int ret;
259
87dc1ab1 260 ret = fio_sgio_doio(td, io_u, io_u->ddir == DDIR_SYNC);
2866c82d
JA
261
262 if (ret < 0)
263 io_u->error = errno;
264 else if (hdr->status) {
265 io_u->resid = hdr->resid;
266 io_u->error = EIO;
267 }
268
269 return io_u->error;
270}
271
272static struct io_u *fio_sgio_event(struct thread_data *td, int event)
273{
274 struct sgio_data *sd = td->io_ops->data;
275
276 return sd->events[event];
277}
278
279static int fio_sgio_get_bs(struct thread_data *td, unsigned int *bs)
280{
281 struct sgio_data *sd = td->io_ops->data;
282 struct io_u *io_u;
283 struct sg_io_hdr *hdr;
284 unsigned char buf[8];
285 int ret;
286
287 io_u = __get_io_u(td);
288 assert(io_u);
289
290 hdr = &io_u->hdr;
291 sgio_hdr_init(sd, hdr, io_u, 0);
292 memset(buf, 0, sizeof(buf));
293
294 hdr->cmdp[0] = 0x25;
295 hdr->dxfer_direction = SG_DXFER_FROM_DEV;
296 hdr->dxferp = buf;
297 hdr->dxfer_len = sizeof(buf);
298
299 ret = fio_sgio_doio(td, io_u, 1);
300 if (ret) {
301 put_io_u(td, io_u);
302 return ret;
303 }
304
305 *bs = (buf[4] << 24) | (buf[5] << 16) | (buf[6] << 8) | buf[7];
306 put_io_u(td, io_u);
307 return 0;
308}
309
310static void fio_sgio_cleanup(struct thread_data *td)
311{
312 if (td->io_ops->data) {
313 free(td->io_ops->data);
314 td->io_ops->data = NULL;
315 }
316}
317
318static int fio_sgio_init(struct thread_data *td)
319{
53cdc686 320 struct fio_file *f = &td->files[0];
2866c82d
JA
321 struct sgio_data *sd;
322 unsigned int bs;
323 int ret;
324
325 sd = malloc(sizeof(*sd));
cb781c75 326 memset(sd, 0, sizeof(*sd));
2866c82d 327 sd->cmds = malloc(td->iodepth * sizeof(struct sgio_cmd));
cb781c75 328 memset(sd->cmds, 0, td->iodepth * sizeof(struct sgio_cmd));
2866c82d 329 sd->events = malloc(td->iodepth * sizeof(struct io_u *));
cb781c75 330 memset(sd->events, 0, td->iodepth * sizeof(struct io_u *));
2866c82d
JA
331 td->io_ops->data = sd;
332
333 if (td->filetype == FIO_TYPE_BD) {
53cdc686 334 if (ioctl(f->fd, BLKSSZGET, &bs) < 0) {
2866c82d 335 td_verror(td, errno);
cb781c75 336 goto err;
2866c82d
JA
337 }
338 } else if (td->filetype == FIO_TYPE_CHAR) {
339 int version;
340
53cdc686 341 if (ioctl(f->fd, SG_GET_VERSION_NUM, &version) < 0) {
2866c82d 342 td_verror(td, errno);
cb781c75 343 goto err;
2866c82d
JA
344 }
345
346 ret = fio_sgio_get_bs(td, &bs);
347 if (ret)
cb781c75 348 goto err;
2866c82d
JA
349 } else {
350 log_err("ioengine sgio only works on block devices\n");
cb781c75 351 goto err;
2866c82d
JA
352 }
353
354 sd->bs = bs;
355
356 if (td->filetype == FIO_TYPE_BD)
357 td->io_ops->getevents = fio_sgio_ioctl_getevents;
358 else
359 td->io_ops->getevents = fio_sgio_getevents;
360
361 /*
362 * we want to do it, regardless of whether odirect is set or not
363 */
364 td->override_sync = 1;
365 return 0;
cb781c75
JA
366err:
367 free(sd->events);
368 free(sd->cmds);
369 free(sd);
e8b8c9f1 370 td->io_ops->data = NULL;
cb781c75 371 return 1;
2866c82d
JA
372}
373
5f350952 374static struct ioengine_ops ioengine = {
2866c82d
JA
375 .name = "sg",
376 .version = FIO_IOOPS_VERSION,
377 .init = fio_sgio_init,
378 .prep = fio_sgio_prep,
379 .queue = fio_sgio_queue,
380 .getevents = fio_sgio_getevents,
381 .event = fio_sgio_event,
382 .cleanup = fio_sgio_cleanup,
b2a15192 383 .flags = FIO_SYNCIO | FIO_RAWIO,
2866c82d 384};
34cfcdaf
JA
385
386#else /* FIO_HAVE_SGIO */
387
388/*
389 * When we have a proper configure system in place, we simply wont build
390 * and install this io engine. For now install a crippled version that
391 * just complains and fails to load.
392 */
393static int fio_sgio_init(struct thread_data fio_unused *td)
394{
395 fprintf(stderr, "fio: sgio not available\n");
396 return 1;
397}
398
5f350952 399static struct ioengine_ops ioengine = {
34cfcdaf
JA
400 .name = "sgio",
401 .version = FIO_IOOPS_VERSION,
402 .init = fio_sgio_init,
403};
404
405#endif
5f350952
JA
406
407static void fio_init fio_sgio_register(void)
408{
409 register_ioengine(&ioengine);
410}
411
412static void fio_exit fio_sgio_unregister(void)
413{
414 unregister_ioengine(&ioengine);
415}