parser: always match the correct option length for posval options
[fio.git] / engines / windowsaio.c
CommitLineData
ecc314ba
BC
1/*
2 * Native Windows async IO engine
93bcfd20 3 * Copyright (C) 2012 Bruce Cran <bruce@cran.org.uk>
ecc314ba
BC
4 */
5
ecc314ba
BC
6#include <stdio.h>
7#include <stdlib.h>
8#include <unistd.h>
9#include <signal.h>
10#include <errno.h>
11#include <windows.h>
12
13#include "../fio.h"
14
ea4500d8
BC
15typedef BOOL (WINAPI *CANCELIOEX)(HANDLE hFile, LPOVERLAPPED lpOverlapped);
16
66c098b8
BC
17int geterrno_from_win_error (DWORD code, int deferrno);
18
67897036
BC
19struct fio_overlapped {
20 OVERLAPPED o;
21 struct io_u *io_u;
22 BOOL io_complete;
40c5db35 23 BOOL io_free;
67897036 24};
ecc314ba 25
9b836561 26struct windowsaio_data {
67897036 27 struct fio_overlapped *ovls;
9b836561 28 struct io_u **aio_events;
67897036 29 HANDLE iothread;
9b836561 30 HANDLE iocomplete_event;
ea4500d8 31 CANCELIOEX pCancelIoEx;
67897036 32 BOOL iothread_running;
9b836561
BC
33};
34
ecc314ba 35struct thread_ctx {
9b836561 36 HANDLE iocp;
ecc314ba
BC
37 struct windowsaio_data *wd;
38};
39
ecc314ba
BC
40static int fio_windowsaio_cancel(struct thread_data *td,
41 struct io_u *io_u);
67897036 42static BOOL timeout_expired(DWORD start_count, DWORD end_count);
ecc314ba
BC
43static int fio_windowsaio_getevents(struct thread_data *td, unsigned int min,
44 unsigned int max, struct timespec *t);
45static struct io_u *fio_windowsaio_event(struct thread_data *td, int event);
46static int fio_windowsaio_queue(struct thread_data *td,
47 struct io_u *io_u);
48static void fio_windowsaio_cleanup(struct thread_data *td);
49static DWORD WINAPI IoCompletionRoutine(LPVOID lpParameter);
50static int fio_windowsaio_init(struct thread_data *td);
51static int fio_windowsaio_open_file(struct thread_data *td, struct fio_file *f);
52static int fio_windowsaio_close_file(struct thread_data fio_unused *td, struct fio_file *f);
66c098b8
BC
53static int win_to_poxix_error(DWORD winerr);
54
55static int win_to_poxix_error(DWORD winerr)
56{
57 switch (winerr)
58 {
59 case ERROR_FILE_NOT_FOUND: return ENOENT;
60 case ERROR_PATH_NOT_FOUND: return ENOENT;
61 case ERROR_ACCESS_DENIED: return EACCES;
62 case ERROR_INVALID_HANDLE: return EBADF;
63 case ERROR_NOT_ENOUGH_MEMORY: return ENOMEM;
64 case ERROR_INVALID_DATA: return EINVAL;
65 case ERROR_OUTOFMEMORY: return ENOMEM;
66 case ERROR_INVALID_DRIVE: return ENODEV;
67 case ERROR_NOT_SAME_DEVICE: return EXDEV;
68 case ERROR_WRITE_PROTECT: return EROFS;
69 case ERROR_BAD_UNIT: return ENODEV;
70 case ERROR_SHARING_VIOLATION: return EACCES;
71 case ERROR_LOCK_VIOLATION: return EACCES;
72 case ERROR_SHARING_BUFFER_EXCEEDED: return ENOLCK;
73 case ERROR_HANDLE_DISK_FULL: return ENOSPC;
74 case ERROR_NOT_SUPPORTED: return ENOSYS;
75 case ERROR_FILE_EXISTS: return EEXIST;
76 case ERROR_CANNOT_MAKE: return EPERM;
77 case ERROR_INVALID_PARAMETER: return EINVAL;
78 case ERROR_NO_PROC_SLOTS: return EAGAIN;
79 case ERROR_BROKEN_PIPE: return EPIPE;
80 case ERROR_OPEN_FAILED: return EIO;
81 case ERROR_NO_MORE_SEARCH_HANDLES: return ENFILE;
82 case ERROR_CALL_NOT_IMPLEMENTED: return ENOSYS;
83 case ERROR_INVALID_NAME: return ENOENT;
84 case ERROR_WAIT_NO_CHILDREN: return ECHILD;
85 case ERROR_CHILD_NOT_COMPLETE: return EBUSY;
86 case ERROR_DIR_NOT_EMPTY: return ENOTEMPTY;
87 case ERROR_SIGNAL_REFUSED: return EIO;
88 case ERROR_BAD_PATHNAME: return ENOENT;
89 case ERROR_SIGNAL_PENDING: return EBUSY;
90 case ERROR_MAX_THRDS_REACHED: return EAGAIN;
91 case ERROR_BUSY: return EBUSY;
92 case ERROR_ALREADY_EXISTS: return EEXIST;
93 case ERROR_NO_SIGNAL_SENT: return EIO;
94 case ERROR_FILENAME_EXCED_RANGE: return EINVAL;
95 case ERROR_META_EXPANSION_TOO_LONG: return EINVAL;
96 case ERROR_INVALID_SIGNAL_NUMBER: return EINVAL;
97 case ERROR_THREAD_1_INACTIVE: return EINVAL;
98 case ERROR_BAD_PIPE: return EINVAL;
99 case ERROR_PIPE_BUSY: return EBUSY;
100 case ERROR_NO_DATA: return EPIPE;
101 case ERROR_MORE_DATA: return EAGAIN;
102 case ERROR_DIRECTORY: return ENOTDIR;
103 case ERROR_PIPE_CONNECTED: return EBUSY;
104 case ERROR_NO_TOKEN: return EINVAL;
105 case ERROR_PROCESS_ABORTED: return EFAULT;
106 case ERROR_BAD_DEVICE: return ENODEV;
107 case ERROR_BAD_USERNAME: return EINVAL;
108 case ERROR_OPEN_FILES: return EAGAIN;
109 case ERROR_ACTIVE_CONNECTIONS: return EAGAIN;
110 case ERROR_DEVICE_IN_USE: return EAGAIN;
111 case ERROR_INVALID_AT_INTERRUPT_TIME: return EINTR;
112 case ERROR_IO_DEVICE: return EIO;
113 case ERROR_NOT_OWNER: return EPERM;
114 case ERROR_END_OF_MEDIA: return ENOSPC;
115 case ERROR_EOM_OVERFLOW: return ENOSPC;
116 case ERROR_BEGINNING_OF_MEDIA: return ESPIPE;
117 case ERROR_SETMARK_DETECTED: return ESPIPE;
118 case ERROR_NO_DATA_DETECTED: return ENOSPC;
119 case ERROR_POSSIBLE_DEADLOCK: return EDEADLOCK;
120 case ERROR_CRC: return EIO;
121 case ERROR_NEGATIVE_SEEK: return EINVAL;
122 case ERROR_DISK_FULL: return ENOSPC;
123 case ERROR_NOACCESS: return EFAULT;
124 case ERROR_FILE_INVALID: return ENXIO;
125 }
126
127 return winerr;
128}
ecc314ba 129
9b836561
BC
130int sync_file_range(int fd, off64_t offset, off64_t nbytes,
131 unsigned int flags)
132{
133 errno = ENOSYS;
134 return -1;
135}
136
ecc314ba
BC
137static int fio_windowsaio_init(struct thread_data *td)
138{
ecc314ba 139 struct windowsaio_data *wd;
ea4500d8 140 HANDLE hKernel32Dll;
9b836561 141 int rc = 0;
67897036 142 int i;
ecc314ba 143
ecc314ba 144 wd = malloc(sizeof(struct windowsaio_data));
9b836561
BC
145 if (wd != NULL)
146 ZeroMemory(wd, sizeof(struct windowsaio_data));
147 else
148 rc = 1;
ecc314ba 149
9b836561
BC
150 if (!rc) {
151 wd->aio_events = malloc(td->o.iodepth * sizeof(struct io_u*));
152 if (wd->aio_events == NULL)
153 rc = 1;
e4db9fec
BC
154 }
155
9b836561 156 if (!rc) {
67897036
BC
157 wd->ovls = malloc(td->o.iodepth * sizeof(struct fio_overlapped));
158 if (wd->ovls == NULL)
9b836561 159 rc = 1;
e4db9fec
BC
160 }
161
9b836561 162 if (!rc) {
f9a58c2a
BC
163 for (i = 0; i < td->o.iodepth; i++) {
164 wd->ovls[i].io_free = TRUE;
165 wd->ovls[i].io_complete = FALSE;
67897036
BC
166
167 wd->ovls[i].o.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
168 if (wd->ovls[i].o.hEvent == NULL) {
169 rc = 1;
170 break;
171 }
f9a58c2a 172 }
9b836561 173 }
e4db9fec 174
9b836561
BC
175 if (!rc) {
176 /* Create an auto-reset event */
177 wd->iocomplete_event = CreateEvent(NULL, FALSE, FALSE, NULL);
178 if (wd->iocomplete_event == NULL)
179 rc = 1;
180 }
181
9b836561 182 if (rc) {
9b836561
BC
183 if (wd != NULL) {
184 if (wd->ovls != NULL)
185 free(wd->ovls);
9b836561
BC
186 if (wd->aio_events != NULL)
187 free(wd->aio_events);
188
189 free(wd);
190 }
191 }
ecc314ba 192
ea4500d8 193 hKernel32Dll = GetModuleHandle("kernel32.dll");
93bcfd20 194 wd->pCancelIoEx = (CANCELIOEX)GetProcAddress(hKernel32Dll, "CancelIoEx");
ecc314ba 195 td->io_ops->data = wd;
93bcfd20 196
4e79098f 197 return rc;
ecc314ba
BC
198}
199
67897036
BC
200static void fio_windowsaio_cleanup(struct thread_data *td)
201{
202 int i;
203 struct windowsaio_data *wd;
204
205 wd = td->io_ops->data;
206
207 if (wd != NULL) {
40c5db35
JA
208 wd->iothread_running = FALSE;
209 WaitForSingleObject(wd->iothread, INFINITE);
67897036
BC
210
211 CloseHandle(wd->iothread);
212 CloseHandle(wd->iocomplete_event);
213
93bcfd20 214 for (i = 0; i < td->o.iodepth; i++) {
67897036 215 CloseHandle(wd->ovls[i].o.hEvent);
93bcfd20 216 }
67897036
BC
217
218 free(wd->aio_events);
219 free(wd->ovls);
220 free(wd);
221
222 td->io_ops->data = NULL;
223 }
224}
225
226
ecc314ba
BC
227static int fio_windowsaio_open_file(struct thread_data *td, struct fio_file *f)
228{
229 int rc = 0;
230 HANDLE hFile;
4e79098f 231 DWORD flags = FILE_FLAG_POSIX_SEMANTICS | FILE_FLAG_OVERLAPPED;
ecc314ba
BC
232 DWORD sharemode = FILE_SHARE_READ | FILE_SHARE_WRITE;
233 DWORD openmode = OPEN_ALWAYS;
234 DWORD access;
235
236 dprint(FD_FILE, "fd open %s\n", f->file_name);
237
ecc314ba
BC
238 if (f->filetype == FIO_TYPE_PIPE) {
239 log_err("fio: windowsaio doesn't support pipes\n");
240 return 1;
241 }
242
243 if (!strcmp(f->file_name, "-")) {
244 log_err("fio: can't read/write to stdin/out\n");
245 return 1;
246 }
247
248 if (td->o.odirect)
249 flags |= FILE_FLAG_NO_BUFFERING;
250 if (td->o.sync_io)
251 flags |= FILE_FLAG_WRITE_THROUGH;
252
93bcfd20
BC
253 /*
254 * Inform Windows whether we're going to be doing sequential or
255 * random io so it can tune the Cache Manager
256 */
ecc314ba 257 if (td->o.td_ddir == TD_DDIR_READ ||
ea4500d8 258 td->o.td_ddir == TD_DDIR_WRITE)
ecc314ba 259 flags |= FILE_FLAG_SEQUENTIAL_SCAN;
ecc314ba 260 else
ecc314ba 261 flags |= FILE_FLAG_RANDOM_ACCESS;
ecc314ba 262
ea4500d8 263 if (!td_write(td) || read_only)
ecc314ba
BC
264 access = GENERIC_READ;
265 else
266 access = (GENERIC_READ | GENERIC_WRITE);
267
93bcfd20 268 if (td->o.create_on_open)
ecc314ba
BC
269 openmode = OPEN_ALWAYS;
270 else
271 openmode = OPEN_EXISTING;
272
273 f->hFile = CreateFile(f->file_name, access, sharemode,
274 NULL, openmode, flags, NULL);
275
4e79098f 276 if (f->hFile == INVALID_HANDLE_VALUE)
ecc314ba 277 rc = 1;
ecc314ba 278
93bcfd20 279 /* Only set up the completion port and thread if we're not just
ecc314ba 280 * querying the device size */
40c5db35 281 if (!rc && td->io_ops->data != NULL) {
ecc314ba 282 struct thread_ctx *ctx;
40c5db35 283 struct windowsaio_data *wd;
ecc314ba 284
40c5db35 285 hFile = CreateIoCompletionPort(f->hFile, NULL, 0, 0);
ea4500d8 286
40c5db35 287 wd = td->io_ops->data;
9b836561 288 wd->iothread_running = TRUE;
ecc314ba 289
9b836561
BC
290 if (!rc) {
291 ctx = malloc(sizeof(struct thread_ctx));
292 ctx->iocp = hFile;
293 ctx->wd = wd;
294
295 wd->iothread = CreateThread(NULL, 0, IoCompletionRoutine, ctx, 0, NULL);
296 }
ecc314ba 297
4e79098f 298 if (rc || wd->iothread == NULL)
ecc314ba 299 rc = 1;
ecc314ba
BC
300 }
301
ecc314ba
BC
302 return rc;
303}
304
305static int fio_windowsaio_close_file(struct thread_data fio_unused *td, struct fio_file *f)
306{
4e79098f 307 int rc = 0;
1e42cc3d 308
9b836561
BC
309 dprint(FD_FILE, "fd close %s\n", f->file_name);
310
ecc314ba 311 if (f->hFile != INVALID_HANDLE_VALUE) {
9b836561 312 if (!CloseHandle(f->hFile))
4e79098f 313 rc = 1;
ecc314ba
BC
314 }
315
316 f->hFile = INVALID_HANDLE_VALUE;
4e79098f 317 return rc;
ecc314ba
BC
318}
319
67897036
BC
320static BOOL timeout_expired(DWORD start_count, DWORD end_count)
321{
322 BOOL expired = FALSE;
323 DWORD current_time;
324
325 current_time = GetTickCount();
326
327 if ((end_count > start_count) && current_time >= end_count)
328 expired = TRUE;
329 else if (current_time < start_count && current_time > end_count)
330 expired = TRUE;
331
332 return expired;
333}
334
335static struct io_u* fio_windowsaio_event(struct thread_data *td, int event)
336{
337 struct windowsaio_data *wd = td->io_ops->data;
338 return wd->aio_events[event];
339}
340
341static int fio_windowsaio_getevents(struct thread_data *td, unsigned int min,
342 unsigned int max, struct timespec *t)
343{
344 struct windowsaio_data *wd = td->io_ops->data;
345 struct flist_head *entry;
346 unsigned int dequeued = 0;
347 struct io_u *io_u;
348 struct fio_overlapped *fov;
349 DWORD start_count = 0;
350 DWORD end_count = 0;
351 DWORD status;
352 DWORD mswait = 250;
353
354 if (t != NULL) {
355 mswait = (t->tv_sec * 1000) + (t->tv_nsec / 1000000);
356 start_count = GetTickCount();
357 end_count = start_count + (t->tv_sec * 1000) + (t->tv_nsec / 1000000);
358 }
359
360 do {
361 flist_for_each(entry, &td->io_u_busylist) {
362 io_u = flist_entry(entry, struct io_u, list);
363 fov = (struct fio_overlapped*)io_u->engine_data;
364
365 if (fov->io_complete) {
40c5db35 366 fov->io_complete = FALSE;
67897036 367 fov->io_free = TRUE;
f9a58c2a 368 ResetEvent(fov->o.hEvent);
67897036
BC
369 wd->aio_events[dequeued] = io_u;
370 dequeued++;
371 }
372
373 if (dequeued >= min)
374 break;
375 }
376
40c5db35 377 if (dequeued < min) {
67897036 378 status = WaitForSingleObject(wd->iocomplete_event, mswait);
f9a58c2a 379 if (status != WAIT_OBJECT_0 && dequeued >= min)
67897036
BC
380 break;
381 }
382
383 if (dequeued >= min || (t != NULL && timeout_expired(start_count, end_count)))
384 break;
385 } while (1);
386
387 return dequeued;
388}
389
40c5db35 390static int fio_windowsaio_queue(struct thread_data *td, struct io_u *io_u)
67897036 391{
1e42cc3d 392 LPOVERLAPPED lpOvl = NULL;
67897036
BC
393 struct windowsaio_data *wd;
394 DWORD iobytes;
93bcfd20 395 BOOL success = FALSE;
67897036
BC
396 int index;
397 int rc = FIO_Q_COMPLETED;
398
399 fio_ro_check(td, io_u);
400
401 wd = td->io_ops->data;
402
1e42cc3d 403 for (index = 0; index < td->o.iodepth; index++) {
f9a58c2a 404 if (wd->ovls[index].io_free)
1e42cc3d 405 break;
1e42cc3d 406 }
67897036 407
1e42cc3d 408 assert(index < td->o.iodepth);
67897036 409
f9a58c2a 410 wd->ovls[index].io_free = FALSE;
1e42cc3d 411 wd->ovls[index].io_u = io_u;
f9a58c2a 412 lpOvl = &wd->ovls[index].o;
4e79098f
BC
413 lpOvl->Internal = STATUS_PENDING;
414 lpOvl->InternalHigh = 0;
415 lpOvl->Offset = io_u->offset & 0xFFFFFFFF;
416 lpOvl->OffsetHigh = io_u->offset >> 32;
1e42cc3d 417 io_u->engine_data = &wd->ovls[index];
67897036
BC
418
419 switch (io_u->ddir) {
40c5db35 420 case DDIR_WRITE:
67897036
BC
421 success = WriteFile(io_u->file->hFile, io_u->xfer_buf, io_u->xfer_buflen, &iobytes, lpOvl);
422 break;
423 case DDIR_READ:
424 success = ReadFile(io_u->file->hFile, io_u->xfer_buf, io_u->xfer_buflen, &iobytes, lpOvl);
425 break;
426 case DDIR_SYNC:
427 case DDIR_DATASYNC:
428 case DDIR_SYNC_FILE_RANGE:
429 success = FlushFileBuffers(io_u->file->hFile);
430 if (!success)
66c098b8 431 io_u->error = win_to_poxix_error(GetLastError());
67897036
BC
432
433 return FIO_Q_COMPLETED;
434 break;
435 case DDIR_TRIM:
436 log_err("manual TRIM isn't supported on Windows");
437 io_u->error = 1;
438 io_u->resid = io_u->xfer_buflen;
439 return FIO_Q_COMPLETED;
440 break;
441 default:
442 assert(0);
93bcfd20 443 break;
67897036
BC
444 }
445
40c5db35 446 if (success || GetLastError() == ERROR_IO_PENDING)
67897036 447 rc = FIO_Q_QUEUED;
40c5db35 448 else {
66c098b8 449 io_u->error = win_to_poxix_error(GetLastError());
67897036
BC
450 io_u->resid = io_u->xfer_buflen;
451 }
452
453 return rc;
454}
455
456/* Runs as a thread and waits for queued IO to complete */
457static DWORD WINAPI IoCompletionRoutine(LPVOID lpParameter)
458{
459 OVERLAPPED *ovl;
460 struct fio_overlapped *fov;
461 struct io_u *io_u;
462 struct windowsaio_data *wd;
463 struct thread_ctx *ctx;
464 ULONG_PTR ulKey = 0;
465 DWORD bytes;
466
467 ctx = (struct thread_ctx*)lpParameter;
468 wd = ctx->wd;
469
470 do {
66c098b8 471 if (!GetQueuedCompletionStatus(ctx->iocp, &bytes, &ulKey, &ovl, 250) && ovl == NULL)
67897036
BC
472 continue;
473
474 fov = CONTAINING_RECORD(ovl, struct fio_overlapped, o);
475 io_u = fov->io_u;
476
477 if (ovl->Internal == ERROR_SUCCESS) {
478 io_u->resid = io_u->xfer_buflen - ovl->InternalHigh;
479 io_u->error = 0;
480 } else {
481 io_u->resid = io_u->xfer_buflen;
66c098b8 482 io_u->error = win_to_poxix_error(GetLastError());
67897036
BC
483 }
484
40c5db35 485 fov->io_complete = TRUE;
67897036
BC
486 SetEvent(wd->iocomplete_event);
487 } while (ctx->wd->iothread_running);
488
489 CloseHandle(ctx->iocp);
490 free(ctx);
491 return 0;
492}
493
494static int fio_windowsaio_cancel(struct thread_data *td,
495 struct io_u *io_u)
496{
497 int rc = 0;
498
499 struct windowsaio_data *wd = td->io_ops->data;
500
501 /* If we're running on Vista or newer, we can cancel individual IO requests */
502 if (wd->pCancelIoEx != NULL) {
503 struct fio_overlapped *ovl = io_u->engine_data;
40c5db35 504
67897036
BC
505 if (!wd->pCancelIoEx(io_u->file->hFile, &ovl->o))
506 rc = 1;
507 } else
508 rc = 1;
509
510 return rc;
511}
512
ecc314ba
BC
513static struct ioengine_ops ioengine = {
514 .name = "windowsaio",
515 .version = FIO_IOOPS_VERSION,
516 .init = fio_windowsaio_init,
517 .queue = fio_windowsaio_queue,
518 .cancel = fio_windowsaio_cancel,
519 .getevents = fio_windowsaio_getevents,
520 .event = fio_windowsaio_event,
521 .cleanup = fio_windowsaio_cleanup,
522 .open_file = fio_windowsaio_open_file,
523 .close_file = fio_windowsaio_close_file,
524 .get_file_size = generic_get_file_size
525};
526
527static void fio_init fio_posixaio_register(void)
528{
529 register_ioengine(&ioengine);
530}
531
532static void fio_exit fio_posixaio_unregister(void)
533{
534 unregister_ioengine(&ioengine);
535}