engines/sg: ensure we complete the right command for sync IO
[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 5 *
b4b9665e
VF
6 * This ioengine can operate in two modes:
7 * sync with block devices (/dev/sdX) or
8 * with character devices (/dev/sgY) with direct=1 or sync=1
9 * async with character devices with direct=0 and sync=0
10 *
11 * What value does queue() return for the different cases?
12 * queue() return value
13 * In sync mode:
14 * /dev/sdX RWT FIO_Q_COMPLETED
15 * /dev/sgY RWT FIO_Q_COMPLETED
16 * with direct=1 or sync=1
17 *
18 * In async mode:
19 * /dev/sgY RWT FIO_Q_QUEUED
20 * direct=0 and sync=0
21 *
22 * Because FIO_SYNCIO is set for this ioengine td_io_queue() will fill in
23 * issue_time *before* each IO is sent to queue()
24 *
25 * Where are the IO counting functions called for the different cases?
26 *
27 * In sync mode:
28 * /dev/sdX (commit==NULL)
29 * RWT
30 * io_u_mark_depth() called in td_io_queue()
31 * io_u_mark_submit/complete() called in td_io_queue()
32 * issue_time set in td_io_queue()
33 *
34 * /dev/sgY with direct=1 or sync=1 (commit does nothing)
35 * RWT
36 * io_u_mark_depth() called in td_io_queue()
37 * io_u_mark_submit/complete() called in queue()
38 * issue_time set in td_io_queue()
39 *
40 * In async mode:
41 * /dev/sgY with direct=0 and sync=0
42 * RW: read and write operations are submitted in queue()
43 * io_u_mark_depth() called in td_io_commit()
44 * io_u_mark_submit() called in queue()
45 * issue_time set in td_io_queue()
46 * T: trim operations are queued in queue() and submitted in commit()
47 * io_u_mark_depth() called in td_io_commit()
48 * io_u_mark_submit() called in commit()
49 * issue_time set in commit()
50 *
2866c82d
JA
51 */
52#include <stdio.h>
53#include <stdlib.h>
54#include <unistd.h>
55#include <errno.h>
8393ca93 56#include <poll.h>
5f350952
JA
57
58#include "../fio.h"
52b81b7c 59#include "../optgroup.h"
2866c82d 60
34cfcdaf
JA
61#ifdef FIO_HAVE_SGIO
62
cbdc9353
VF
63enum {
64 FIO_SG_WRITE = 1,
65 FIO_SG_WRITE_VERIFY = 2,
66 FIO_SG_WRITE_SAME = 3
67};
52b81b7c
KD
68
69struct sg_options {
70 void *pad;
71 unsigned int readfua;
72 unsigned int writefua;
cbdc9353 73 unsigned int write_mode;
52b81b7c
KD
74};
75
76static struct fio_option options[] = {
77 {
78 .name = "readfua",
79 .lname = "sg engine read fua flag support",
80 .type = FIO_OPT_BOOL,
81 .off1 = offsetof(struct sg_options, readfua),
82 .help = "Set FUA flag (force unit access) for all Read operations",
83 .def = "0",
84 .category = FIO_OPT_C_ENGINE,
85 .group = FIO_OPT_G_SG,
86 },
87 {
88 .name = "writefua",
89 .lname = "sg engine write fua flag support",
90 .type = FIO_OPT_BOOL,
91 .off1 = offsetof(struct sg_options, writefua),
92 .help = "Set FUA flag (force unit access) for all Write operations",
93 .def = "0",
94 .category = FIO_OPT_C_ENGINE,
95 .group = FIO_OPT_G_SG,
96 },
cbdc9353
VF
97 {
98 .name = "sg_write_mode",
99 .lname = "specify sg write mode",
100 .type = FIO_OPT_STR,
101 .off1 = offsetof(struct sg_options, write_mode),
102 .help = "Specify SCSI WRITE mode",
103 .def = "write",
104 .posval = {
105 { .ival = "write",
106 .oval = FIO_SG_WRITE,
107 .help = "Issue standard SCSI WRITE commands",
108 },
109 { .ival = "verify",
110 .oval = FIO_SG_WRITE_VERIFY,
111 .help = "Issue SCSI WRITE AND VERIFY commands",
112 },
113 { .ival = "same",
114 .oval = FIO_SG_WRITE_SAME,
115 .help = "Issue SCSI WRITE SAME commands",
116 },
117 },
118 .category = FIO_OPT_C_ENGINE,
119 .group = FIO_OPT_G_SG,
120 },
52b81b7c
KD
121 {
122 .name = NULL,
123 },
124};
125
5ad7be56
KD
126#define MAX_10B_LBA 0xFFFFFFFFULL
127#define SCSI_TIMEOUT_MS 30000 // 30 second timeout; currently no method to override
128#define MAX_SB 64 // sense block maximum return size
aa18e0ec
VF
129/*
130#define FIO_SGIO_DEBUG
131*/
5ad7be56 132
2866c82d 133struct sgio_cmd {
fde57152 134 unsigned char cdb[16]; // enhanced from 10 to support 16 byte commands
5ad7be56 135 unsigned char sb[MAX_SB]; // add sense block to commands
2866c82d
JA
136 int nr;
137};
138
b4b9665e 139struct sgio_trim {
a824149a 140 uint8_t *unmap_param;
b4b9665e
VF
141 unsigned int unmap_range_count;
142 struct io_u **trim_io_us;
143};
144
2866c82d
JA
145struct sgio_data {
146 struct sgio_cmd *cmds;
147 struct io_u **events;
dc0deca2
JA
148 struct pollfd *pfds;
149 int *fd_flags;
150 void *sgbuf;
2866c82d 151 unsigned int bs;
b5af8293 152 int type_checked;
b4b9665e
VF
153 struct sgio_trim **trim_queues;
154 int current_queue;
aa18e0ec 155#ifdef FIO_SGIO_DEBUG
b4b9665e 156 unsigned int *trim_queue_map;
aa18e0ec 157#endif
2866c82d
JA
158};
159
a824149a
DF
160static inline uint32_t sgio_get_be32(uint8_t *buf)
161{
162 return be32_to_cpu(*((uint32_t *) buf));
163}
164
165static inline uint64_t sgio_get_be64(uint8_t *buf)
166{
167 return be64_to_cpu(*((uint64_t *) buf));
168}
169
170static inline void sgio_set_be16(uint16_t val, uint8_t *buf)
171{
172 uint16_t t = cpu_to_be16(val);
173
174 memcpy(buf, &t, sizeof(uint16_t));
175}
176
177static inline void sgio_set_be32(uint32_t val, uint8_t *buf)
178{
179 uint32_t t = cpu_to_be32(val);
180
181 memcpy(buf, &t, sizeof(uint32_t));
182}
183
184static inline void sgio_set_be64(uint64_t val, uint8_t *buf)
185{
186 uint64_t t = cpu_to_be64(val);
187
188 memcpy(buf, &t, sizeof(uint64_t));
189}
190
b4b9665e
VF
191static inline bool sgio_unbuffered(struct thread_data *td)
192{
193 return (td->o.odirect || td->o.sync_io);
194}
195
2866c82d
JA
196static void sgio_hdr_init(struct sgio_data *sd, struct sg_io_hdr *hdr,
197 struct io_u *io_u, int fs)
198{
199 struct sgio_cmd *sc = &sd->cmds[io_u->index];
200
201 memset(hdr, 0, sizeof(*hdr));
202 memset(sc->cdb, 0, sizeof(sc->cdb));
203
204 hdr->interface_id = 'S';
205 hdr->cmdp = sc->cdb;
206 hdr->cmd_len = sizeof(sc->cdb);
5ad7be56
KD
207 hdr->sbp = sc->sb;
208 hdr->mx_sb_len = sizeof(sc->sb);
2866c82d
JA
209 hdr->pack_id = io_u->index;
210 hdr->usr_ptr = io_u;
b4b9665e 211 hdr->timeout = SCSI_TIMEOUT_MS;
2866c82d
JA
212
213 if (fs) {
cec6b55d
JA
214 hdr->dxferp = io_u->xfer_buf;
215 hdr->dxfer_len = io_u->xfer_buflen;
2866c82d
JA
216 }
217}
218
adee86c5
JA
219static int pollin_events(struct pollfd *pfds, int fds)
220{
221 int i;
222
223 for (i = 0; i < fds; i++)
224 if (pfds[i].revents & POLLIN)
225 return 1;
226
227 return 0;
228}
2866c82d 229
14d0261e
JA
230static int sg_fd_read(int fd, void *data, size_t size)
231{
232 int err = 0;
233
234 while (size) {
235 ssize_t ret;
236
237 ret = read(fd, data, size);
238 if (ret < 0) {
239 if (errno == EAGAIN || errno == EINTR)
240 continue;
241 err = errno;
242 break;
243 } else if (!ret)
244 break;
245 else {
246 data += ret;
247 size -= ret;
248 }
249 }
250
251 if (err)
252 return err;
253 if (size)
254 return EAGAIN;
255
256 return 0;
257}
258
e7d2e616 259static int fio_sgio_getevents(struct thread_data *td, unsigned int min,
1f440ece
JA
260 unsigned int max,
261 const struct timespec fio_unused *t)
2866c82d 262{
565e784d 263 struct sgio_data *sd = td->io_ops_data;
b4b9665e 264 int left = max, eventNum, ret, r = 0, trims = 0;
dc0deca2 265 void *buf = sd->sgbuf;
b4b9665e 266 unsigned int i, j, events;
946ff865 267 struct fio_file *f;
b4b9665e 268 struct io_u *io_u;
2866c82d
JA
269
270 /*
adee86c5 271 * Fill in the file descriptors
2866c82d 272 */
adee86c5
JA
273 for_each_file(td, f, i) {
274 /*
275 * don't block for min events == 0
276 */
4a851614 277 if (!min)
3a35845f
JA
278 sd->fd_flags[i] = fio_set_fd_nonblocking(f->fd, "sg");
279 else
280 sd->fd_flags[i] = -1;
4a851614 281
dc0deca2
JA
282 sd->pfds[i].fd = f->fd;
283 sd->pfds[i].events = POLLIN;
2866c82d
JA
284 }
285
b4b9665e
VF
286 /*
287 ** There are two counters here:
288 ** - number of SCSI commands completed
289 ** - number of io_us completed
290 **
291 ** These are the same with reads and writes, but
292 ** could differ with trim/unmap commands because
293 ** a single unmap can include multiple io_us
294 */
295
296 while (left > 0) {
c97e3cb0 297 char *p;
adee86c5 298
b4b9665e 299 dprint(FD_IO, "sgio_getevents: sd %p: min=%d, max=%d, left=%d\n", sd, min, max, left);
5ad7be56 300
2866c82d
JA
301 do {
302 if (!min)
303 break;
adee86c5 304
2dc1bbeb 305 ret = poll(sd->pfds, td->o.nr_files, -1);
adee86c5 306 if (ret < 0) {
adee86c5 307 if (!r)
22819ec2 308 r = -errno;
e1161c32 309 td_verror(td, errno, "poll");
adee86c5
JA
310 break;
311 } else if (!ret)
312 continue;
313
2dc1bbeb 314 if (pollin_events(sd->pfds, td->o.nr_files))
2866c82d
JA
315 break;
316 } while (1);
317
adee86c5 318 if (r < 0)
2866c82d 319 break;
adee86c5
JA
320
321re_read:
322 p = buf;
323 events = 0;
324 for_each_file(td, f, i) {
5ad7be56 325 for (eventNum = 0; eventNum < left; eventNum++) {
14d0261e 326 ret = sg_fd_read(f->fd, p, sizeof(struct sg_io_hdr));
b4b9665e 327 dprint(FD_IO, "sgio_getevents: sg_fd_read ret: %d\n", ret);
14d0261e
JA
328 if (ret) {
329 r = -ret;
330 td_verror(td, r, "sg_read");
5ad7be56 331 break;
5ad7be56 332 }
b4b9665e
VF
333 io_u = ((struct sg_io_hdr *)p)->usr_ptr;
334 if (io_u->ddir == DDIR_TRIM) {
335 events += sd->trim_queues[io_u->index]->unmap_range_count;
336 eventNum += sd->trim_queues[io_u->index]->unmap_range_count - 1;
337 } else
338 events++;
339
14d0261e 340 p += sizeof(struct sg_io_hdr);
b4b9665e 341 dprint(FD_IO, "sgio_getevents: events: %d, eventNum: %d, left: %d\n", events, eventNum, left);
adee86c5
JA
342 }
343 }
344
14d0261e 345 if (r < 0 && !events)
2866c82d 346 break;
adee86c5
JA
347 if (!events) {
348 usleep(1000);
349 goto re_read;
350 }
2866c82d 351
2866c82d
JA
352 left -= events;
353 r += events;
354
355 for (i = 0; i < events; i++) {
356 struct sg_io_hdr *hdr = (struct sg_io_hdr *) buf + i;
b4b9665e
VF
357 sd->events[i + trims] = hdr->usr_ptr;
358 io_u = (struct io_u *)(hdr->usr_ptr);
5ad7be56 359
5ad7be56 360 if (hdr->info & SG_INFO_CHECK) {
b4b9665e 361 /* record if an io error occurred, ignore resid */
be660713 362 memcpy(&io_u->hdr, hdr, sizeof(struct sg_io_hdr));
b4b9665e
VF
363 sd->events[i + trims]->error = EIO;
364 }
365
366 if (io_u->ddir == DDIR_TRIM) {
367 struct sgio_trim *st = sd->trim_queues[io_u->index];
aa18e0ec 368#ifdef FIO_SGIO_DEBUG
b4b9665e 369 assert(st->trim_io_us[0] == io_u);
aa18e0ec 370 assert(sd->trim_queue_map[io_u->index] == io_u->index);
b4b9665e
VF
371 dprint(FD_IO, "sgio_getevents: reaping %d io_us from trim queue %d\n", st->unmap_range_count, io_u->index);
372 dprint(FD_IO, "sgio_getevents: reaped io_u %d and stored in events[%d]\n", io_u->index, i+trims);
aa18e0ec 373#endif
b4b9665e
VF
374 for (j = 1; j < st->unmap_range_count; j++) {
375 ++trims;
376 sd->events[i + trims] = st->trim_io_us[j];
aa18e0ec 377#ifdef FIO_SGIO_DEBUG
b4b9665e 378 dprint(FD_IO, "sgio_getevents: reaped io_u %d and stored in events[%d]\n", st->trim_io_us[j]->index, i+trims);
aa18e0ec
VF
379 assert(sd->trim_queue_map[st->trim_io_us[j]->index] == io_u->index);
380#endif
b4b9665e
VF
381 if (hdr->info & SG_INFO_CHECK) {
382 /* record if an io error occurred, ignore resid */
383 memcpy(&st->trim_io_us[j]->hdr, hdr, sizeof(struct sg_io_hdr));
384 sd->events[i + trims]->error = EIO;
385 }
386 }
387 events -= st->unmap_range_count - 1;
388 st->unmap_range_count = 0;
5ad7be56 389 }
2866c82d
JA
390 }
391 }
392
adee86c5 393 if (!min) {
affe05a9 394 for_each_file(td, f, i) {
3a35845f
JA
395 if (sd->fd_flags[i] == -1)
396 continue;
397
affe05a9
JA
398 if (fcntl(f->fd, F_SETFL, sd->fd_flags[i]) < 0)
399 log_err("fio: sg failed to restore fcntl flags: %s\n", strerror(errno));
400 }
adee86c5 401 }
2866c82d 402
2866c82d
JA
403 return r;
404}
405
2e4ef4fb
JA
406static enum fio_q_status fio_sgio_ioctl_doio(struct thread_data *td,
407 struct fio_file *f,
408 struct io_u *io_u)
2866c82d 409{
565e784d 410 struct sgio_data *sd = td->io_ops_data;
2866c82d 411 struct sg_io_hdr *hdr = &io_u->hdr;
36167d82 412 int ret;
2866c82d
JA
413
414 sd->events[0] = io_u;
415
36167d82
JA
416 ret = ioctl(f->fd, SG_IO, hdr);
417 if (ret < 0)
a05bd42d 418 return ret;
36167d82 419
5ad7be56
KD
420 /* record if an io error occurred */
421 if (hdr->info & SG_INFO_CHECK)
422 io_u->error = EIO;
423
36167d82 424 return FIO_Q_COMPLETED;
2866c82d
JA
425}
426
a999bc49
JA
427static enum fio_q_status fio_sgio_rw_doio(struct thread_data *td,
428 struct fio_file *f,
b4b9665e 429 struct io_u *io_u, int do_sync)
2866c82d
JA
430{
431 struct sg_io_hdr *hdr = &io_u->hdr;
432 int ret;
433
53cdc686 434 ret = write(f->fd, hdr, sizeof(*hdr));
2866c82d 435 if (ret < 0)
a05bd42d 436 return ret;
2866c82d 437
2b13e716 438 if (do_sync) {
a999bc49
JA
439 /*
440 * We can't just read back the first command that completes
441 * and assume it's the one we need, it could be any command
442 * that is inflight.
443 */
444 do {
445 struct io_u *__io_u;
446
447 ret = read(f->fd, hdr, sizeof(*hdr));
448 if (ret < 0)
449 return ret;
450
451 /* record if an io error occurred */
452 if (hdr->info & SG_INFO_CHECK)
453 io_u->error = EIO;
5ad7be56 454
a999bc49
JA
455 __io_u = hdr->usr_ptr;
456 if (__io_u == io_u)
457 break;
458
459 if (io_u_sync_complete(td, __io_u)) {
460 ret = -1;
461 break;
462 }
463 } while (1);
5ad7be56 464
36167d82 465 return FIO_Q_COMPLETED;
2866c82d
JA
466 }
467
36167d82 468 return FIO_Q_QUEUED;
2866c82d
JA
469}
470
b4b9665e
VF
471static enum fio_q_status fio_sgio_doio(struct thread_data *td,
472 struct io_u *io_u, int do_sync)
2866c82d 473{
53cdc686 474 struct fio_file *f = io_u->file;
b4b9665e 475 enum fio_q_status ret;
53cdc686 476
686fbd31 477 if (f->filetype == FIO_TYPE_BLOCK) {
5ad7be56 478 ret = fio_sgio_ioctl_doio(td, f, io_u);
a999bc49
JA
479 if (io_u->error)
480 td_verror(td, io_u->error, __func__);
5ad7be56 481 } else {
a999bc49
JA
482 ret = fio_sgio_rw_doio(td, f, io_u, do_sync);
483 if (io_u->error && do_sync)
c9aeb797 484 td_verror(td, io_u->error, __func__);
5ad7be56 485 }
2866c82d 486
5ad7be56 487 return ret;
2866c82d
JA
488}
489
b4b9665e
VF
490static void fio_sgio_rw_lba(struct sg_io_hdr *hdr, unsigned long long lba,
491 unsigned long long nr_blocks)
492{
493 if (lba < MAX_10B_LBA) {
a824149a
DF
494 sgio_set_be32((uint32_t) lba, &hdr->cmdp[2]);
495 sgio_set_be16((uint16_t) nr_blocks, &hdr->cmdp[7]);
b4b9665e 496 } else {
a824149a
DF
497 sgio_set_be64(lba, &hdr->cmdp[2]);
498 sgio_set_be32((uint32_t) nr_blocks, &hdr->cmdp[10]);
b4b9665e
VF
499 }
500
501 return;
502}
503
2866c82d
JA
504static int fio_sgio_prep(struct thread_data *td, struct io_u *io_u)
505{
506 struct sg_io_hdr *hdr = &io_u->hdr;
52b81b7c 507 struct sg_options *o = td->eo;
565e784d 508 struct sgio_data *sd = td->io_ops_data;
b4b9665e
VF
509 unsigned long long nr_blocks, lba;
510 int offset;
2866c82d 511
cec6b55d 512 if (io_u->xfer_buflen & (sd->bs - 1)) {
2866c82d
JA
513 log_err("read/write not sector aligned\n");
514 return EINVAL;
515 }
516
5ad7be56
KD
517 nr_blocks = io_u->xfer_buflen / sd->bs;
518 lba = io_u->offset / sd->bs;
519
2866c82d 520 if (io_u->ddir == DDIR_READ) {
87dc1ab1
JA
521 sgio_hdr_init(sd, hdr, io_u, 1);
522
2866c82d 523 hdr->dxfer_direction = SG_DXFER_FROM_DEV;
5ad7be56
KD
524 if (lba < MAX_10B_LBA)
525 hdr->cmdp[0] = 0x28; // read(10)
526 else
527 hdr->cmdp[0] = 0x88; // read(16)
52b81b7c
KD
528
529 if (o->readfua)
530 hdr->cmdp[1] |= 0x08;
531
b4b9665e
VF
532 fio_sgio_rw_lba(hdr, lba, nr_blocks);
533
87dc1ab1
JA
534 } else if (io_u->ddir == DDIR_WRITE) {
535 sgio_hdr_init(sd, hdr, io_u, 1);
536
2866c82d 537 hdr->dxfer_direction = SG_DXFER_TO_DEV;
cbdc9353
VF
538 switch(o->write_mode) {
539 case FIO_SG_WRITE:
540 if (lba < MAX_10B_LBA)
541 hdr->cmdp[0] = 0x2a; // write(10)
542 else
543 hdr->cmdp[0] = 0x8a; // write(16)
544 if (o->writefua)
545 hdr->cmdp[1] |= 0x08;
546 break;
547 case FIO_SG_WRITE_VERIFY:
548 if (lba < MAX_10B_LBA)
549 hdr->cmdp[0] = 0x2e; // write and verify(10)
550 else
551 hdr->cmdp[0] = 0x8e; // write and verify(16)
552 break;
553 // BYTCHK is disabled by virtue of the memset in sgio_hdr_init
554 case FIO_SG_WRITE_SAME:
555 hdr->dxfer_len = sd->bs;
556 if (lba < MAX_10B_LBA)
557 hdr->cmdp[0] = 0x41; // write same(10)
558 else
559 hdr->cmdp[0] = 0x93; // write same(16)
560 break;
561 };
b4b9665e
VF
562
563 fio_sgio_rw_lba(hdr, lba, nr_blocks);
564
565 } else if (io_u->ddir == DDIR_TRIM) {
566 struct sgio_trim *st;
567
568 if (sd->current_queue == -1) {
569 sgio_hdr_init(sd, hdr, io_u, 0);
570
571 hdr->cmd_len = 10;
572 hdr->dxfer_direction = SG_DXFER_TO_DEV;
573 hdr->cmdp[0] = 0x42; // unmap
574 sd->current_queue = io_u->index;
575 st = sd->trim_queues[sd->current_queue];
576 hdr->dxferp = st->unmap_param;
aa18e0ec 577#ifdef FIO_SGIO_DEBUG
b4b9665e
VF
578 assert(sd->trim_queues[io_u->index]->unmap_range_count == 0);
579 dprint(FD_IO, "sg: creating new queue based on io_u %d\n", io_u->index);
aa18e0ec 580#endif
b4b9665e
VF
581 }
582 else
583 st = sd->trim_queues[sd->current_queue];
584
585 dprint(FD_IO, "sg: adding io_u %d to trim queue %d\n", io_u->index, sd->current_queue);
586 st->trim_io_us[st->unmap_range_count] = io_u;
aa18e0ec 587#ifdef FIO_SGIO_DEBUG
b4b9665e 588 sd->trim_queue_map[io_u->index] = sd->current_queue;
aa18e0ec 589#endif
b4b9665e
VF
590
591 offset = 8 + 16 * st->unmap_range_count;
a824149a
DF
592 sgio_set_be64(lba, &st->unmap_param[offset]);
593 sgio_set_be32((uint32_t) nr_blocks, &st->unmap_param[offset + 8]);
b4b9665e
VF
594
595 st->unmap_range_count++;
596
597 } else if (ddir_sync(io_u->ddir)) {
87dc1ab1 598 sgio_hdr_init(sd, hdr, io_u, 0);
87dc1ab1 599 hdr->dxfer_direction = SG_DXFER_NONE;
5ad7be56
KD
600 if (lba < MAX_10B_LBA)
601 hdr->cmdp[0] = 0x35; // synccache(10)
602 else
603 hdr->cmdp[0] = 0x91; // synccache(16)
b4b9665e
VF
604 } else
605 assert(0);
2866c82d 606
2866c82d
JA
607 return 0;
608}
609
b4b9665e
VF
610static void fio_sgio_unmap_setup(struct sg_io_hdr *hdr, struct sgio_trim *st)
611{
a824149a 612 uint16_t cnt = st->unmap_range_count * 16;
b4b9665e 613
a824149a
DF
614 hdr->dxfer_len = cnt + 8;
615 sgio_set_be16(cnt + 8, &hdr->cmdp[7]);
616 sgio_set_be16(cnt + 6, st->unmap_param);
617 sgio_set_be16(cnt, &st->unmap_param[2]);
b4b9665e
VF
618
619 return;
620}
621
2e4ef4fb
JA
622static enum fio_q_status fio_sgio_queue(struct thread_data *td,
623 struct io_u *io_u)
2866c82d
JA
624{
625 struct sg_io_hdr *hdr = &io_u->hdr;
b4b9665e 626 struct sgio_data *sd = td->io_ops_data;
f6db4fa5 627 int ret, do_sync = 0;
2866c82d 628
7101d9c2
JA
629 fio_ro_check(td, io_u);
630
b4b9665e 631 if (sgio_unbuffered(td) || ddir_sync(io_u->ddir))
f6db4fa5
JA
632 do_sync = 1;
633
b4b9665e
VF
634 if (io_u->ddir == DDIR_TRIM) {
635 if (do_sync || io_u->file->filetype == FIO_TYPE_BLOCK) {
636 struct sgio_trim *st = sd->trim_queues[sd->current_queue];
637
638 /* finish cdb setup for unmap because we are
639 ** doing unmap commands synchronously */
aa18e0ec 640#ifdef FIO_SGIO_DEBUG
b4b9665e
VF
641 assert(st->unmap_range_count == 1);
642 assert(io_u == st->trim_io_us[0]);
aa18e0ec 643#endif
b4b9665e
VF
644 hdr = &io_u->hdr;
645
646 fio_sgio_unmap_setup(hdr, st);
647
648 st->unmap_range_count = 0;
649 sd->current_queue = -1;
650 } else
651 /* queue up trim ranges and submit in commit() */
652 return FIO_Q_QUEUED;
653 }
654
f6db4fa5 655 ret = fio_sgio_doio(td, io_u, do_sync);
2866c82d
JA
656
657 if (ret < 0)
658 io_u->error = errno;
659 else if (hdr->status) {
660 io_u->resid = hdr->resid;
661 io_u->error = EIO;
b4b9665e
VF
662 } else if (td->io_ops->commit != NULL) {
663 if (do_sync && !ddir_sync(io_u->ddir)) {
664 io_u_mark_submit(td, 1);
665 io_u_mark_complete(td, 1);
666 } else if (io_u->ddir == DDIR_READ || io_u->ddir == DDIR_WRITE) {
667 io_u_mark_submit(td, 1);
668 io_u_queued(td, io_u);
669 }
2866c82d
JA
670 }
671
95bcd815 672 if (io_u->error) {
e1161c32 673 td_verror(td, io_u->error, "xfer");
36167d82 674 return FIO_Q_COMPLETED;
95bcd815
JA
675 }
676
36167d82 677 return ret;
2866c82d
JA
678}
679
b4b9665e
VF
680static int fio_sgio_commit(struct thread_data *td)
681{
682 struct sgio_data *sd = td->io_ops_data;
683 struct sgio_trim *st;
684 struct io_u *io_u;
685 struct sg_io_hdr *hdr;
686 struct timespec now;
687 unsigned int i;
688 int ret;
689
690 if (sd->current_queue == -1)
691 return 0;
692
693 st = sd->trim_queues[sd->current_queue];
694 io_u = st->trim_io_us[0];
695 hdr = &io_u->hdr;
696
697 fio_sgio_unmap_setup(hdr, st);
698
699 sd->current_queue = -1;
700
a999bc49 701 ret = fio_sgio_rw_doio(td, io_u->file, io_u, 0);
b4b9665e 702
53ee8c17
VF
703 if (ret < 0 || hdr->status) {
704 int error;
705
706 if (ret < 0)
707 error = errno;
708 else {
709 error = EIO;
710 ret = -EIO;
b4b9665e 711 }
53ee8c17
VF
712
713 for (i = 0; i < st->unmap_range_count; i++) {
714 st->trim_io_us[i]->error = error;
715 clear_io_u(td, st->trim_io_us[i]);
716 if (hdr->status)
717 st->trim_io_us[i]->resid = hdr->resid;
b4b9665e 718 }
53ee8c17
VF
719
720 td_verror(td, error, "xfer");
721 return ret;
b4b9665e
VF
722 }
723
53ee8c17
VF
724 if (fio_fill_issue_time(td)) {
725 fio_gettime(&now, NULL);
726 for (i = 0; i < st->unmap_range_count; i++) {
727 memcpy(&st->trim_io_us[i]->issue_time, &now, sizeof(now));
728 io_u_queued(td, io_u);
729 }
b4b9665e 730 }
53ee8c17 731 io_u_mark_submit(td, st->unmap_range_count);
b4b9665e 732
53ee8c17 733 return 0;
b4b9665e
VF
734}
735
2866c82d
JA
736static struct io_u *fio_sgio_event(struct thread_data *td, int event)
737{
565e784d 738 struct sgio_data *sd = td->io_ops_data;
2866c82d
JA
739
740 return sd->events[event];
741}
742
5ad7be56
KD
743static int fio_sgio_read_capacity(struct thread_data *td, unsigned int *bs,
744 unsigned long long *max_lba)
2866c82d 745{
5ad7be56
KD
746 /*
747 * need to do read capacity operation w/o benefit of sd or
748 * io_u structures, which are not initialized until later.
749 */
750 struct sg_io_hdr hdr;
02ae7bd8
DF
751 unsigned long long hlba;
752 unsigned int blksz = 0;
5ad7be56
KD
753 unsigned char cmd[16];
754 unsigned char sb[64];
755 unsigned char buf[32]; // read capacity return
2866c82d 756 int ret;
5ad7be56 757 int fd = -1;
2866c82d 758
5ad7be56 759 struct fio_file *f = td->files[0];
2866c82d 760
5ad7be56
KD
761 /* open file independent of rest of application */
762 fd = open(f->file_name, O_RDONLY);
763 if (fd < 0)
764 return -errno;
2866c82d 765
5ad7be56
KD
766 memset(&hdr, 0, sizeof(hdr));
767 memset(cmd, 0, sizeof(cmd));
768 memset(sb, 0, sizeof(sb));
769 memset(buf, 0, sizeof(buf));
2866c82d 770
5ad7be56
KD
771 /* First let's try a 10 byte read capacity. */
772 hdr.interface_id = 'S';
773 hdr.cmdp = cmd;
774 hdr.cmd_len = 10;
775 hdr.sbp = sb;
776 hdr.mx_sb_len = sizeof(sb);
777 hdr.timeout = SCSI_TIMEOUT_MS;
778 hdr.cmdp[0] = 0x25; // Read Capacity(10)
779 hdr.dxfer_direction = SG_DXFER_FROM_DEV;
780 hdr.dxferp = buf;
781 hdr.dxfer_len = sizeof(buf);
782
783 ret = ioctl(fd, SG_IO, &hdr);
784 if (ret < 0) {
785 close(fd);
2866c82d 786 return ret;
5ad7be56 787 }
2866c82d 788
02ae7bd8
DF
789 if (hdr.info & SG_INFO_CHECK) {
790 /* RCAP(10) might be unsupported by device. Force RCAP(16) */
791 hlba = MAX_10B_LBA;
792 } else {
a824149a
DF
793 blksz = sgio_get_be32(&buf[4]);
794 hlba = sgio_get_be32(buf);
02ae7bd8 795 }
5ad7be56
KD
796
797 /*
fde57152
TK
798 * If max lba masked by MAX_10B_LBA equals MAX_10B_LBA,
799 * then need to retry with 16 byte Read Capacity command.
5ad7be56 800 */
02ae7bd8 801 if (hlba == MAX_10B_LBA) {
5ad7be56 802 hdr.cmd_len = 16;
28c43a89
TK
803 hdr.cmdp[0] = 0x9e; // service action
804 hdr.cmdp[1] = 0x10; // Read Capacity(16)
a824149a 805 sgio_set_be32(sizeof(buf), &hdr.cmdp[10]);
5ad7be56
KD
806
807 hdr.dxfer_direction = SG_DXFER_FROM_DEV;
808 hdr.dxferp = buf;
809 hdr.dxfer_len = sizeof(buf);
810
811 ret = ioctl(fd, SG_IO, &hdr);
812 if (ret < 0) {
813 close(fd);
814 return ret;
815 }
816
817 /* record if an io error occurred */
818 if (hdr.info & SG_INFO_CHECK)
819 td_verror(td, EIO, "fio_sgio_read_capacity");
820
a824149a
DF
821 blksz = sgio_get_be32(&buf[8]);
822 hlba = sgio_get_be64(buf);
02ae7bd8
DF
823 }
824
825 if (blksz) {
826 *bs = blksz;
827 *max_lba = hlba;
828 ret = 0;
829 } else {
830 ret = EIO;
5ad7be56
KD
831 }
832
833 close(fd);
02ae7bd8 834 return ret;
2866c82d
JA
835}
836
837static void fio_sgio_cleanup(struct thread_data *td)
838{
565e784d 839 struct sgio_data *sd = td->io_ops_data;
b4b9665e 840 int i;
dc0deca2
JA
841
842 if (sd) {
843 free(sd->events);
844 free(sd->cmds);
845 free(sd->fd_flags);
846 free(sd->pfds);
847 free(sd->sgbuf);
aa18e0ec 848#ifdef FIO_SGIO_DEBUG
b4b9665e 849 free(sd->trim_queue_map);
aa18e0ec 850#endif
b4b9665e
VF
851
852 for (i = 0; i < td->o.iodepth; i++) {
853 free(sd->trim_queues[i]->unmap_param);
854 free(sd->trim_queues[i]->trim_io_us);
855 free(sd->trim_queues[i]);
856 }
857
858 free(sd->trim_queues);
dc0deca2 859 free(sd);
2866c82d
JA
860 }
861}
862
863static int fio_sgio_init(struct thread_data *td)
864{
865 struct sgio_data *sd;
b4b9665e
VF
866 struct sgio_trim *st;
867 int i;
2866c82d 868
b4b9665e
VF
869 sd = calloc(1, sizeof(*sd));
870 sd->cmds = calloc(td->o.iodepth, sizeof(struct sgio_cmd));
871 sd->sgbuf = calloc(td->o.iodepth, sizeof(struct sg_io_hdr));
872 sd->events = calloc(td->o.iodepth, sizeof(struct io_u *));
873 sd->pfds = calloc(td->o.nr_files, sizeof(struct pollfd));
874 sd->fd_flags = calloc(td->o.nr_files, sizeof(int));
5ad7be56 875 sd->type_checked = 0;
b4b9665e
VF
876
877 sd->trim_queues = calloc(td->o.iodepth, sizeof(struct sgio_trim *));
878 sd->current_queue = -1;
aa18e0ec 879#ifdef FIO_SGIO_DEBUG
b4b9665e 880 sd->trim_queue_map = calloc(td->o.iodepth, sizeof(int));
aa18e0ec 881#endif
b4b9665e
VF
882 for (i = 0; i < td->o.iodepth; i++) {
883 sd->trim_queues[i] = calloc(1, sizeof(struct sgio_trim));
884 st = sd->trim_queues[i];
885 st->unmap_param = calloc(td->o.iodepth + 1, sizeof(char[16]));
886 st->unmap_range_count = 0;
887 st->trim_io_us = calloc(td->o.iodepth, sizeof(struct io_u *));
888 }
889
565e784d 890 td->io_ops_data = sd;
2866c82d 891
b5af8293
JA
892 /*
893 * we want to do it, regardless of whether odirect is set or not
894 */
2dc1bbeb 895 td->o.override_sync = 1;
b5af8293
JA
896 return 0;
897}
898
899static int fio_sgio_type_check(struct thread_data *td, struct fio_file *f)
900{
565e784d 901 struct sgio_data *sd = td->io_ops_data;
5ad7be56
KD
902 unsigned int bs = 0;
903 unsigned long long max_lba = 0;
904
686fbd31 905 if (f->filetype == FIO_TYPE_BLOCK) {
53cdc686 906 if (ioctl(f->fd, BLKSSZGET, &bs) < 0) {
e1161c32 907 td_verror(td, errno, "ioctl");
b5af8293 908 return 1;
2866c82d 909 }
af52b345 910 } else if (f->filetype == FIO_TYPE_CHAR) {
b5af8293 911 int version, ret;
2866c82d 912
53cdc686 913 if (ioctl(f->fd, SG_GET_VERSION_NUM, &version) < 0) {
e1161c32 914 td_verror(td, errno, "ioctl");
b5af8293 915 return 1;
2866c82d
JA
916 }
917
5ad7be56
KD
918 ret = fio_sgio_read_capacity(td, &bs, &max_lba);
919 if (ret) {
920 td_verror(td, td->error, "fio_sgio_read_capacity");
921 log_err("ioengine sg unable to read capacity successfully\n");
b5af8293 922 return 1;
5ad7be56 923 }
2866c82d 924 } else {
16ada754 925 td_verror(td, EINVAL, "wrong file type");
30dac136 926 log_err("ioengine sg only works on block or character devices\n");
b5af8293 927 return 1;
2866c82d
JA
928 }
929
930 sd->bs = bs;
5ad7be56 931 // Determine size of commands needed based on max_lba
166c6b42
TK
932 if (max_lba >= MAX_10B_LBA) {
933 dprint(FD_IO, "sgio_type_check: using 16 byte read/write "
934 "commands for lba above 0x%016llx/0x%016llx\n",
935 MAX_10B_LBA, max_lba);
5ad7be56
KD
936 }
937
686fbd31 938 if (f->filetype == FIO_TYPE_BLOCK) {
36167d82
JA
939 td->io_ops->getevents = NULL;
940 td->io_ops->event = NULL;
b4b9665e
VF
941 td->io_ops->commit = NULL;
942 /*
943 ** Setting these functions to null may cause problems
944 ** with filename=/dev/sda:/dev/sg0 since we are only
945 ** considering a single file
946 */
36167d82 947 }
5ad7be56 948 sd->type_checked = 1;
2866c82d 949
2866c82d 950 return 0;
b5af8293
JA
951}
952
953static int fio_sgio_open(struct thread_data *td, struct fio_file *f)
954{
565e784d 955 struct sgio_data *sd = td->io_ops_data;
b5af8293
JA
956 int ret;
957
958 ret = generic_open_file(td, f);
959 if (ret)
960 return ret;
961
15ba640a 962 if (sd && !sd->type_checked && fio_sgio_type_check(td, f)) {
6977bcd0 963 ret = generic_close_file(td, f);
b5af8293
JA
964 return 1;
965 }
966
967 return 0;
2866c82d
JA
968}
969
5ad7be56
KD
970/*
971 * Build an error string with details about the driver, host or scsi
972 * error contained in the sg header Caller will use as necessary.
973 */
974static char *fio_sgio_errdetails(struct io_u *io_u)
975{
976 struct sg_io_hdr *hdr = &io_u->hdr;
977#define MAXERRDETAIL 1024
978#define MAXMSGCHUNK 128
fd04fa03 979 char *msg, msgchunk[MAXMSGCHUNK];
5ad7be56
KD
980 int i;
981
efa72f25 982 msg = calloc(1, MAXERRDETAIL);
fd04fa03 983 strcpy(msg, "");
5ad7be56
KD
984
985 /*
986 * can't seem to find sg_err.h, so I'll just echo the define values
987 * so others can search on internet to find clearer clues of meaning.
988 */
989 if (hdr->info & SG_INFO_CHECK) {
5ad7be56
KD
990 if (hdr->host_status) {
991 snprintf(msgchunk, MAXMSGCHUNK, "SG Host Status: 0x%02x; ", hdr->host_status);
992 strlcat(msg, msgchunk, MAXERRDETAIL);
993 switch (hdr->host_status) {
994 case 0x01:
995 strlcat(msg, "SG_ERR_DID_NO_CONNECT", MAXERRDETAIL);
996 break;
997 case 0x02:
998 strlcat(msg, "SG_ERR_DID_BUS_BUSY", MAXERRDETAIL);
999 break;
1000 case 0x03:
1001 strlcat(msg, "SG_ERR_DID_TIME_OUT", MAXERRDETAIL);
1002 break;
1003 case 0x04:
1004 strlcat(msg, "SG_ERR_DID_BAD_TARGET", MAXERRDETAIL);
1005 break;
1006 case 0x05:
1007 strlcat(msg, "SG_ERR_DID_ABORT", MAXERRDETAIL);
1008 break;
1009 case 0x06:
1010 strlcat(msg, "SG_ERR_DID_PARITY", MAXERRDETAIL);
1011 break;
1012 case 0x07:
1013 strlcat(msg, "SG_ERR_DID_ERROR (internal error)", MAXERRDETAIL);
1014 break;
1015 case 0x08:
1016 strlcat(msg, "SG_ERR_DID_RESET", MAXERRDETAIL);
1017 break;
1018 case 0x09:
1019 strlcat(msg, "SG_ERR_DID_BAD_INTR (unexpected)", MAXERRDETAIL);
1020 break;
1021 case 0x0a:
1022 strlcat(msg, "SG_ERR_DID_PASSTHROUGH", MAXERRDETAIL);
1023 break;
1024 case 0x0b:
1025 strlcat(msg, "SG_ERR_DID_SOFT_ERROR (driver retry?)", MAXERRDETAIL);
1026 break;
1027 case 0x0c:
1028 strlcat(msg, "SG_ERR_DID_IMM_RETRY", MAXERRDETAIL);
1029 break;
1030 case 0x0d:
1031 strlcat(msg, "SG_ERR_DID_REQUEUE", MAXERRDETAIL);
1032 break;
2ce6c6e5
TK
1033 case 0x0e:
1034 strlcat(msg, "SG_ERR_DID_TRANSPORT_DISRUPTED", MAXERRDETAIL);
1035 break;
1036 case 0x0f:
1037 strlcat(msg, "SG_ERR_DID_TRANSPORT_FAILFAST", MAXERRDETAIL);
1038 break;
1039 case 0x10:
1040 strlcat(msg, "SG_ERR_DID_TARGET_FAILURE", MAXERRDETAIL);
1041 break;
1042 case 0x11:
1043 strlcat(msg, "SG_ERR_DID_NEXUS_FAILURE", MAXERRDETAIL);
1044 break;
1045 case 0x12:
1046 strlcat(msg, "SG_ERR_DID_ALLOC_FAILURE", MAXERRDETAIL);
1047 break;
1048 case 0x13:
1049 strlcat(msg, "SG_ERR_DID_MEDIUM_ERROR", MAXERRDETAIL);
1050 break;
5ad7be56
KD
1051 default:
1052 strlcat(msg, "Unknown", MAXERRDETAIL);
1053 break;
1054 }
1055 strlcat(msg, ". ", MAXERRDETAIL);
1056 }
1057 if (hdr->driver_status) {
1058 snprintf(msgchunk, MAXMSGCHUNK, "SG Driver Status: 0x%02x; ", hdr->driver_status);
1059 strlcat(msg, msgchunk, MAXERRDETAIL);
1060 switch (hdr->driver_status & 0x0F) {
1061 case 0x01:
1062 strlcat(msg, "SG_ERR_DRIVER_BUSY", MAXERRDETAIL);
1063 break;
1064 case 0x02:
1065 strlcat(msg, "SG_ERR_DRIVER_SOFT", MAXERRDETAIL);
1066 break;
1067 case 0x03:
1068 strlcat(msg, "SG_ERR_DRIVER_MEDIA", MAXERRDETAIL);
1069 break;
1070 case 0x04:
1071 strlcat(msg, "SG_ERR_DRIVER_ERROR", MAXERRDETAIL);
1072 break;
1073 case 0x05:
1074 strlcat(msg, "SG_ERR_DRIVER_INVALID", MAXERRDETAIL);
1075 break;
1076 case 0x06:
1077 strlcat(msg, "SG_ERR_DRIVER_TIMEOUT", MAXERRDETAIL);
1078 break;
1079 case 0x07:
1080 strlcat(msg, "SG_ERR_DRIVER_HARD", MAXERRDETAIL);
1081 break;
1082 case 0x08:
1083 strlcat(msg, "SG_ERR_DRIVER_SENSE", MAXERRDETAIL);
1084 break;
1085 default:
1086 strlcat(msg, "Unknown", MAXERRDETAIL);
1087 break;
1088 }
1089 strlcat(msg, "; ", MAXERRDETAIL);
1090 switch (hdr->driver_status & 0xF0) {
1091 case 0x10:
1092 strlcat(msg, "SG_ERR_SUGGEST_RETRY", MAXERRDETAIL);
1093 break;
1094 case 0x20:
1095 strlcat(msg, "SG_ERR_SUGGEST_ABORT", MAXERRDETAIL);
1096 break;
1097 case 0x30:
1098 strlcat(msg, "SG_ERR_SUGGEST_REMAP", MAXERRDETAIL);
1099 break;
1100 case 0x40:
1101 strlcat(msg, "SG_ERR_SUGGEST_DIE", MAXERRDETAIL);
1102 break;
1103 case 0x80:
1104 strlcat(msg, "SG_ERR_SUGGEST_SENSE", MAXERRDETAIL);
1105 break;
1106 }
1107 strlcat(msg, ". ", MAXERRDETAIL);
1108 }
1109 if (hdr->status) {
1110 snprintf(msgchunk, MAXMSGCHUNK, "SG SCSI Status: 0x%02x; ", hdr->status);
1111 strlcat(msg, msgchunk, MAXERRDETAIL);
1112 // SCSI 3 status codes
1113 switch (hdr->status) {
1114 case 0x02:
1115 strlcat(msg, "CHECK_CONDITION", MAXERRDETAIL);
1116 break;
1117 case 0x04:
1118 strlcat(msg, "CONDITION_MET", MAXERRDETAIL);
1119 break;
1120 case 0x08:
1121 strlcat(msg, "BUSY", MAXERRDETAIL);
1122 break;
1123 case 0x10:
1124 strlcat(msg, "INTERMEDIATE", MAXERRDETAIL);
1125 break;
1126 case 0x14:
1127 strlcat(msg, "INTERMEDIATE_CONDITION_MET", MAXERRDETAIL);
1128 break;
1129 case 0x18:
1130 strlcat(msg, "RESERVATION_CONFLICT", MAXERRDETAIL);
1131 break;
1132 case 0x22:
1133 strlcat(msg, "COMMAND_TERMINATED", MAXERRDETAIL);
1134 break;
1135 case 0x28:
1136 strlcat(msg, "TASK_SET_FULL", MAXERRDETAIL);
1137 break;
1138 case 0x30:
1139 strlcat(msg, "ACA_ACTIVE", MAXERRDETAIL);
1140 break;
1141 case 0x40:
1142 strlcat(msg, "TASK_ABORTED", MAXERRDETAIL);
1143 break;
1144 default:
1145 strlcat(msg, "Unknown", MAXERRDETAIL);
1146 break;
1147 }
1148 strlcat(msg, ". ", MAXERRDETAIL);
1149 }
1150 if (hdr->sb_len_wr) {
1151 snprintf(msgchunk, MAXMSGCHUNK, "Sense Data (%d bytes):", hdr->sb_len_wr);
1152 strlcat(msg, msgchunk, MAXERRDETAIL);
1153 for (i = 0; i < hdr->sb_len_wr; i++) {
1154 snprintf(msgchunk, MAXMSGCHUNK, " %02x", hdr->sbp[i]);
1155 strlcat(msg, msgchunk, MAXERRDETAIL);
1156 }
1157 strlcat(msg, ". ", MAXERRDETAIL);
1158 }
1159 if (hdr->resid != 0) {
1160 snprintf(msgchunk, MAXMSGCHUNK, "SG Driver: %d bytes out of %d not transferred. ", hdr->resid, hdr->dxfer_len);
1161 strlcat(msg, msgchunk, MAXERRDETAIL);
5ad7be56 1162 }
b4dbb3ce
VF
1163 if (hdr->cmdp) {
1164 strlcat(msg, "cdb:", MAXERRDETAIL);
1165 for (i = 0; i < hdr->cmd_len; i++) {
1166 snprintf(msgchunk, MAXMSGCHUNK, " %02x", hdr->cmdp[i]);
1167 strlcat(msg, msgchunk, MAXERRDETAIL);
1168 }
1169 strlcat(msg, ". ", MAXERRDETAIL);
1170 if (io_u->ddir == DDIR_TRIM) {
1171 unsigned char *param_list = hdr->dxferp;
1172 strlcat(msg, "dxferp:", MAXERRDETAIL);
1173 for (i = 0; i < hdr->dxfer_len; i++) {
1174 snprintf(msgchunk, MAXMSGCHUNK, " %02x", param_list[i]);
1175 strlcat(msg, msgchunk, MAXERRDETAIL);
1176 }
1177 strlcat(msg, ". ", MAXERRDETAIL);
1178 }
1179 }
5ad7be56
KD
1180 }
1181
fd04fa03
TK
1182 if (!(hdr->info & SG_INFO_CHECK) && !strlen(msg))
1183 strncpy(msg, "SG Driver did not report a Host, Driver or Device check",
1184 MAXERRDETAIL - 1);
5ad7be56 1185
fd04fa03 1186 return msg;
5ad7be56
KD
1187}
1188
1189/*
1190 * get max file size from read capacity.
1191 */
1192static int fio_sgio_get_file_size(struct thread_data *td, struct fio_file *f)
1193{
1194 /*
1195 * get_file_size is being called even before sgio_init is
1196 * called, so none of the sg_io structures are
1197 * initialized in the thread_data yet. So we need to do the
1198 * ReadCapacity without any of those helpers. One of the effects
1199 * is that ReadCapacity may get called 4 times on each open:
1200 * readcap(10) followed by readcap(16) if needed - just to get
1201 * the file size after the init occurs - it will be called
1202 * again when "type_check" is called during structure
1203 * initialization I'm not sure how to prevent this little
1204 * inefficiency.
1205 */
1206 unsigned int bs = 0;
1207 unsigned long long max_lba = 0;
1208 int ret;
1209
1210 if (fio_file_size_known(f))
1211 return 0;
1212
686fbd31 1213 if (f->filetype != FIO_TYPE_BLOCK && f->filetype != FIO_TYPE_CHAR) {
30dac136
TK
1214 td_verror(td, EINVAL, "wrong file type");
1215 log_err("ioengine sg only works on block or character devices\n");
1216 return 1;
1217 }
1218
5ad7be56
KD
1219 ret = fio_sgio_read_capacity(td, &bs, &max_lba);
1220 if (ret ) {
1221 td_verror(td, td->error, "fio_sgio_read_capacity");
1222 log_err("ioengine sg unable to successfully execute read capacity to get block size and maximum lba\n");
1223 return 1;
1224 }
1225
1226 f->real_file_size = (max_lba + 1) * bs;
1227 fio_file_set_size_known(f);
1228 return 0;
1229}
1230
1231
5f350952 1232static struct ioengine_ops ioengine = {
2866c82d
JA
1233 .name = "sg",
1234 .version = FIO_IOOPS_VERSION,
1235 .init = fio_sgio_init,
1236 .prep = fio_sgio_prep,
1237 .queue = fio_sgio_queue,
b4b9665e 1238 .commit = fio_sgio_commit,
2866c82d 1239 .getevents = fio_sgio_getevents,
5ad7be56 1240 .errdetails = fio_sgio_errdetails,
2866c82d
JA
1241 .event = fio_sgio_event,
1242 .cleanup = fio_sgio_cleanup,
b5af8293
JA
1243 .open_file = fio_sgio_open,
1244 .close_file = generic_close_file,
fde57152 1245 .get_file_size = fio_sgio_get_file_size,
b2a15192 1246 .flags = FIO_SYNCIO | FIO_RAWIO,
52b81b7c
KD
1247 .options = options,
1248 .option_struct_size = sizeof(struct sg_options)
2866c82d 1249};
34cfcdaf
JA
1250
1251#else /* FIO_HAVE_SGIO */
1252
1253/*
1254 * When we have a proper configure system in place, we simply wont build
1255 * and install this io engine. For now install a crippled version that
1256 * just complains and fails to load.
1257 */
1258static int fio_sgio_init(struct thread_data fio_unused *td)
1259{
a3edaf76 1260 log_err("fio: ioengine sg not available\n");
34cfcdaf
JA
1261 return 1;
1262}
1263
5f350952 1264static struct ioengine_ops ioengine = {
d0c70934 1265 .name = "sg",
34cfcdaf
JA
1266 .version = FIO_IOOPS_VERSION,
1267 .init = fio_sgio_init,
1268};
1269
1270#endif
5f350952
JA
1271
1272static void fio_init fio_sgio_register(void)
1273{
1274 register_ioengine(&ioengine);
1275}
1276
1277static void fio_exit fio_sgio_unregister(void)
1278{
1279 unregister_ioengine(&ioengine);
1280}