[PATCH] SG IO engine: remember to set io_u->file in init
[fio.git] / engines / sg.c
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>
11
12 #include "../fio.h"
13 #include "../os.h"
14
15 #ifdef FIO_HAVE_SGIO
16
17 struct sgio_cmd {
18         unsigned char cdb[10];
19         int nr;
20 };
21
22 struct sgio_data {
23         struct sgio_cmd *cmds;
24         struct io_u **events;
25         unsigned int bs;
26 };
27
28 static 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) {
43                 hdr->dxferp = io_u->xfer_buf;
44                 hdr->dxfer_len = io_u->xfer_buflen;
45         }
46 }
47
48 static 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
63 static 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 }
73
74 static int fio_sgio_getevents(struct thread_data *td, int min, int max,
75                               struct timespec fio_unused *t)
76 {
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          */
81         struct fio_file *f = &td->files[0];
82         struct sgio_data *sd = td->io_ops->data;
83         int left = max, ret, events, i, r = 0, *fl;
84         struct pollfd *pfds;
85         void *buf;
86
87         /*
88          * Fill in the file descriptors
89          */
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;
103         }
104
105         buf = malloc(max * sizeof(struct sg_io_hdr));
106         while (left) {
107                 void *p;
108
109                 do {
110                         if (!min)
111                                 break;
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))
123                                 break;
124                 } while (1);
125
126                 if (r < 0)
127                         break;
128
129 re_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)
147                         break;
148                 if (!events) {
149                         usleep(1000);
150                         goto re_read;
151                 }
152
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
163         if (!min) {
164                 for_each_file(td, f, i)
165                         fcntl(f->fd, F_SETFL, fl[i]);
166         }
167
168         free(buf);
169         free(pfds);
170         free(fl);
171         return r;
172 }
173
174 static int fio_sgio_ioctl_doio(struct thread_data *td,
175                                struct fio_file *f, struct io_u *io_u)
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
182         return ioctl(f->fd, SG_IO, hdr);
183 }
184
185 static int fio_sgio_rw_doio(struct fio_file *f, struct io_u *io_u, int sync)
186 {
187         struct sg_io_hdr *hdr = &io_u->hdr;
188         int ret;
189
190         ret = write(f->fd, hdr, sizeof(*hdr));
191         if (ret < 0)
192                 return errno;
193
194         if (sync) {
195                 ret = read(f->fd, hdr, sizeof(*hdr));
196                 if (ret < 0)
197                         return errno;
198         }
199
200         return 0;
201 }
202
203 static int fio_sgio_doio(struct thread_data *td, struct io_u *io_u, int sync)
204 {
205         struct fio_file *f = io_u->file;
206
207         if (td->filetype == FIO_TYPE_BD)
208                 return fio_sgio_ioctl_doio(td, f, io_u);
209
210         return fio_sgio_rw_doio(f, io_u, sync);
211 }
212
213 static 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
219         if (io_u->xfer_buflen & (sd->bs - 1)) {
220                 log_err("read/write not sector aligned\n");
221                 return EINVAL;
222         }
223
224         if (io_u->ddir == DDIR_READ) {
225                 sgio_hdr_init(sd, hdr, io_u, 1);
226
227                 hdr->dxfer_direction = SG_DXFER_FROM_DEV;
228                 hdr->cmdp[0] = 0x28;
229         } else if (io_u->ddir == DDIR_WRITE) {
230                 sgio_hdr_init(sd, hdr, io_u, 1);
231
232                 hdr->dxfer_direction = SG_DXFER_TO_DEV;
233                 hdr->cmdp[0] = 0x2a;
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) {
242                 nr_blocks = io_u->xfer_buflen / sd->bs;
243                 lba = io_u->offset / sd->bs;
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);
250         }
251
252         return 0;
253 }
254
255 static 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
260         ret = fio_sgio_doio(td, io_u, io_u->ddir == DDIR_SYNC);
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
272 static 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
279 static 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         io_u->file = &td->files[0];
289         assert(io_u);
290
291         hdr = &io_u->hdr;
292         sgio_hdr_init(sd, hdr, io_u, 0);
293         memset(buf, 0, sizeof(buf));
294
295         hdr->cmdp[0] = 0x25;
296         hdr->dxfer_direction = SG_DXFER_FROM_DEV;
297         hdr->dxferp = buf;
298         hdr->dxfer_len = sizeof(buf);
299
300         ret = fio_sgio_doio(td, io_u, 1);
301         if (ret) {
302                 put_io_u(td, io_u);
303                 return ret;
304         }
305
306         *bs = (buf[4] << 24) | (buf[5] << 16) | (buf[6] << 8) | buf[7];
307         put_io_u(td, io_u);
308         return 0;
309 }
310
311 static void fio_sgio_cleanup(struct thread_data *td)
312 {
313         if (td->io_ops->data) {
314                 free(td->io_ops->data);
315                 td->io_ops->data = NULL;
316         }
317 }
318
319 static int fio_sgio_init(struct thread_data *td)
320 {
321         struct fio_file *f = &td->files[0];
322         struct sgio_data *sd;
323         unsigned int bs;
324         int ret;
325
326         sd = malloc(sizeof(*sd));
327         memset(sd, 0, sizeof(*sd));
328         sd->cmds = malloc(td->iodepth * sizeof(struct sgio_cmd));
329         memset(sd->cmds, 0, td->iodepth * sizeof(struct sgio_cmd));
330         sd->events = malloc(td->iodepth * sizeof(struct io_u *));
331         memset(sd->events, 0, td->iodepth * sizeof(struct io_u *));
332         td->io_ops->data = sd;
333
334         if (td->filetype == FIO_TYPE_BD) {
335                 if (ioctl(f->fd, BLKSSZGET, &bs) < 0) {
336                         td_verror(td, errno);
337                         goto err;
338                 }
339         } else if (td->filetype == FIO_TYPE_CHAR) {
340                 int version;
341
342                 if (ioctl(f->fd, SG_GET_VERSION_NUM, &version) < 0) {
343                         td_verror(td, errno);
344                         goto err;
345                 }
346
347                 ret = fio_sgio_get_bs(td, &bs);
348                 if (ret)
349                         goto err;
350         } else {
351                 log_err("ioengine sgio only works on block devices\n");
352                 goto err;
353         }
354
355         sd->bs = bs;
356
357         if (td->filetype == FIO_TYPE_BD)
358                 td->io_ops->getevents = fio_sgio_ioctl_getevents;
359         else
360                 td->io_ops->getevents = fio_sgio_getevents;
361
362         /*
363          * we want to do it, regardless of whether odirect is set or not
364          */
365         td->override_sync = 1;
366         return 0;
367 err:
368         free(sd->events);
369         free(sd->cmds);
370         free(sd);
371         td->io_ops->data = NULL;
372         return 1;
373 }
374
375 static struct ioengine_ops ioengine = {
376         .name           = "sg",
377         .version        = FIO_IOOPS_VERSION,
378         .init           = fio_sgio_init,
379         .prep           = fio_sgio_prep,
380         .queue          = fio_sgio_queue,
381         .getevents      = fio_sgio_getevents,
382         .event          = fio_sgio_event,
383         .cleanup        = fio_sgio_cleanup,
384         .flags          = FIO_SYNCIO | FIO_RAWIO,
385 };
386
387 #else /* FIO_HAVE_SGIO */
388
389 /*
390  * When we have a proper configure system in place, we simply wont build
391  * and install this io engine. For now install a crippled version that
392  * just complains and fails to load.
393  */
394 static int fio_sgio_init(struct thread_data fio_unused *td)
395 {
396         fprintf(stderr, "fio: sgio not available\n");
397         return 1;
398 }
399
400 static struct ioengine_ops ioengine = {
401         .name           = "sgio",
402         .version        = FIO_IOOPS_VERSION,
403         .init           = fio_sgio_init,
404 };
405
406 #endif
407
408 static void fio_init fio_sgio_register(void)
409 {
410         register_ioengine(&ioengine);
411 }
412
413 static void fio_exit fio_sgio_unregister(void)
414 {
415         unregister_ioengine(&ioengine);
416 }