windowsaio: fix file header format and improve error reporting.
[fio.git] / engines / windowsaio.c
CommitLineData
ecc314ba 1/*
03244c19
BC
2 * windowsaio engine
3 *
4 * IO engine using Windows IO Completion Ports.
ecc314ba
BC
5 */
6
ecc314ba
BC
7#include <stdio.h>
8#include <stdlib.h>
9#include <unistd.h>
10#include <signal.h>
11#include <errno.h>
12#include <windows.h>
13
14#include "../fio.h"
15
ea4500d8
BC
16typedef BOOL (WINAPI *CANCELIOEX)(HANDLE hFile, LPOVERLAPPED lpOverlapped);
17
66c098b8
BC
18int geterrno_from_win_error (DWORD code, int deferrno);
19
67897036
BC
20struct fio_overlapped {
21 OVERLAPPED o;
22 struct io_u *io_u;
23 BOOL io_complete;
67897036 24};
ecc314ba 25
9b836561 26struct windowsaio_data {
9b836561 27 struct io_u **aio_events;
5a90bb5f 28 HANDLE iocp;
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 40static int fio_windowsaio_cancel(struct thread_data *td,
03244c19 41 struct io_u *io_u);
67897036 42static BOOL timeout_expired(DWORD start_count, DWORD end_count);
ecc314ba 43static int fio_windowsaio_getevents(struct thread_data *td, unsigned int min,
03244c19 44 unsigned int max, struct timespec *t);
ecc314ba
BC
45static struct io_u *fio_windowsaio_event(struct thread_data *td, int event);
46static int fio_windowsaio_queue(struct thread_data *td,
03244c19 47 struct io_u *io_u);
ecc314ba
BC
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);
2277d5d5 53static int win_to_posix_error(DWORD winerr);
66c098b8 54
2277d5d5 55static int win_to_posix_error(DWORD winerr)
66c098b8
BC
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
ecc314ba
BC
130static int fio_windowsaio_init(struct thread_data *td)
131{
ecc314ba 132 struct windowsaio_data *wd;
ea4500d8 133 HANDLE hKernel32Dll;
9b836561 134 int rc = 0;
ecc314ba 135
03244c19
BC
136 wd = calloc(1, sizeof(struct windowsaio_data));
137 if (wd == NULL) {
138 log_err("windowsaio: failed to allocate memory for engine data\n");
9b836561 139 rc = 1;
03244c19 140 }
ecc314ba 141
9b836561
BC
142 if (!rc) {
143 wd->aio_events = malloc(td->o.iodepth * sizeof(struct io_u*));
03244c19
BC
144 if (wd->aio_events == NULL) {
145 log_err("windowsaio: failed to allocate memory for aio events list\n");
9b836561 146 rc = 1;
03244c19 147 }
e4db9fec
BC
148 }
149
9b836561
BC
150 if (!rc) {
151 /* Create an auto-reset event */
152 wd->iocomplete_event = CreateEvent(NULL, FALSE, FALSE, NULL);
03244c19
BC
153 if (wd->iocomplete_event == NULL) {
154 log_err("windowsaio: failed to create io complete event handle\n");
9b836561 155 rc = 1;
03244c19 156 }
9b836561
BC
157 }
158
9b836561 159 if (rc) {
9b836561 160 if (wd != NULL) {
9b836561
BC
161 if (wd->aio_events != NULL)
162 free(wd->aio_events);
163
164 free(wd);
165 }
166 }
ecc314ba 167
ea4500d8 168 hKernel32Dll = GetModuleHandle("kernel32.dll");
93bcfd20 169 wd->pCancelIoEx = (CANCELIOEX)GetProcAddress(hKernel32Dll, "CancelIoEx");
ecc314ba 170 td->io_ops->data = wd;
93bcfd20 171
5a90bb5f
BC
172 if (!rc) {
173 struct thread_ctx *ctx;
174 struct windowsaio_data *wd;
175 HANDLE hFile;
176
177 hFile = CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, 0, 0);
03244c19
BC
178 if (hFile == INVALID_HANDLE_VALUE) {
179 log_err("windowsaio: failed to create io completion port\n");
5a90bb5f 180 rc = 1;
03244c19 181 }
5a90bb5f
BC
182
183 wd = td->io_ops->data;
184 wd->iothread_running = TRUE;
185 wd->iocp = hFile;
186
187 if (!rc)
188 ctx = malloc(sizeof(struct thread_ctx));
189
190 if (!rc && ctx == NULL)
191 {
03244c19 192 log_err("windowsaio: failed to allocate memory for thread context structure\n");
5a90bb5f
BC
193 CloseHandle(hFile);
194 rc = 1;
195 }
196
197 if (!rc)
198 {
199 ctx->iocp = hFile;
200 ctx->wd = wd;
201 wd->iothread = CreateThread(NULL, 0, IoCompletionRoutine, ctx, 0, NULL);
03244c19
BC
202 if (wd->iothread == NULL)
203 log_err("windowsaio: failed to create io completion thread\n");
5a90bb5f
BC
204 }
205
206 if (rc || wd->iothread == NULL)
207 rc = 1;
208 }
209
4e79098f 210 return rc;
ecc314ba
BC
211}
212
67897036
BC
213static void fio_windowsaio_cleanup(struct thread_data *td)
214{
67897036
BC
215 struct windowsaio_data *wd;
216
217 wd = td->io_ops->data;
218
219 if (wd != NULL) {
40c5db35
JA
220 wd->iothread_running = FALSE;
221 WaitForSingleObject(wd->iothread, INFINITE);
67897036
BC
222
223 CloseHandle(wd->iothread);
224 CloseHandle(wd->iocomplete_event);
225
67897036 226 free(wd->aio_events);
67897036
BC
227 free(wd);
228
229 td->io_ops->data = NULL;
230 }
231}
232
233
ecc314ba
BC
234static int fio_windowsaio_open_file(struct thread_data *td, struct fio_file *f)
235{
236 int rc = 0;
4e79098f 237 DWORD flags = FILE_FLAG_POSIX_SEMANTICS | FILE_FLAG_OVERLAPPED;
ecc314ba
BC
238 DWORD sharemode = FILE_SHARE_READ | FILE_SHARE_WRITE;
239 DWORD openmode = OPEN_ALWAYS;
240 DWORD access;
241
242 dprint(FD_FILE, "fd open %s\n", f->file_name);
243
ecc314ba 244 if (f->filetype == FIO_TYPE_PIPE) {
03244c19 245 log_err("windowsaio: pipes are not supported\n");
ecc314ba
BC
246 return 1;
247 }
248
249 if (!strcmp(f->file_name, "-")) {
03244c19 250 log_err("windowsaio: can't read/write to stdin/out\n");
ecc314ba
BC
251 return 1;
252 }
253
254 if (td->o.odirect)
255 flags |= FILE_FLAG_NO_BUFFERING;
256 if (td->o.sync_io)
257 flags |= FILE_FLAG_WRITE_THROUGH;
258
93bcfd20
BC
259 /*
260 * Inform Windows whether we're going to be doing sequential or
261 * random io so it can tune the Cache Manager
262 */
ecc314ba 263 if (td->o.td_ddir == TD_DDIR_READ ||
ea4500d8 264 td->o.td_ddir == TD_DDIR_WRITE)
ecc314ba 265 flags |= FILE_FLAG_SEQUENTIAL_SCAN;
ecc314ba 266 else
ecc314ba 267 flags |= FILE_FLAG_RANDOM_ACCESS;
ecc314ba 268
ea4500d8 269 if (!td_write(td) || read_only)
ecc314ba
BC
270 access = GENERIC_READ;
271 else
272 access = (GENERIC_READ | GENERIC_WRITE);
273
93bcfd20 274 if (td->o.create_on_open)
ecc314ba
BC
275 openmode = OPEN_ALWAYS;
276 else
277 openmode = OPEN_EXISTING;
278
279 f->hFile = CreateFile(f->file_name, access, sharemode,
280 NULL, openmode, flags, NULL);
281
03244c19
BC
282 if (f->hFile == INVALID_HANDLE_VALUE) {
283 log_err("windowsaio: failed to open file \"%s\"\n", f->file_name);
ecc314ba 284 rc = 1;
03244c19 285 }
ecc314ba 286
93bcfd20 287 /* Only set up the completion port and thread if we're not just
ecc314ba 288 * querying the device size */
40c5db35 289 if (!rc && td->io_ops->data != NULL) {
40c5db35 290 struct windowsaio_data *wd;
ecc314ba 291
40c5db35 292 wd = td->io_ops->data;
ecc314ba 293
03244c19
BC
294 if (CreateIoCompletionPort(f->hFile, wd->iocp, 0, 0) == NULL) {
295 log_err("windowsaio: failed to create io completion port\n");
ecc314ba 296 rc = 1;
03244c19 297 }
ecc314ba
BC
298 }
299
ecc314ba
BC
300 return rc;
301}
302
303static int fio_windowsaio_close_file(struct thread_data fio_unused *td, struct fio_file *f)
304{
4e79098f 305 int rc = 0;
1e42cc3d 306
9b836561
BC
307 dprint(FD_FILE, "fd close %s\n", f->file_name);
308
ecc314ba 309 if (f->hFile != INVALID_HANDLE_VALUE) {
03244c19
BC
310 if (!CloseHandle(f->hFile)) {
311 log_info("windowsaio: failed to close file handle for \"%s\"\n", f->file_name);
4e79098f 312 rc = 1;
03244c19 313 }
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,
03244c19 342 unsigned int max, struct timespec *t)
67897036
BC
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;
f9a58c2a 367 ResetEvent(fov->o.hEvent);
67897036
BC
368 wd->aio_events[dequeued] = io_u;
369 dequeued++;
370 }
371
372 if (dequeued >= min)
373 break;
374 }
375
40c5db35 376 if (dequeued < min) {
67897036 377 status = WaitForSingleObject(wd->iocomplete_event, mswait);
f9a58c2a 378 if (status != WAIT_OBJECT_0 && dequeued >= min)
03244c19 379 break;
67897036
BC
380 }
381
382 if (dequeued >= min || (t != NULL && timeout_expired(start_count, end_count)))
383 break;
384 } while (1);
385
386 return dequeued;
387}
388
40c5db35 389static int fio_windowsaio_queue(struct thread_data *td, struct io_u *io_u)
67897036 390{
c73ed246
JA
391 struct fio_overlapped *o = io_u->engine_data;
392 LPOVERLAPPED lpOvl = &o->o;
67897036 393 DWORD iobytes;
93bcfd20 394 BOOL success = FALSE;
67897036
BC
395 int rc = FIO_Q_COMPLETED;
396
397 fio_ro_check(td, io_u);
398
4e79098f
BC
399 lpOvl->Internal = STATUS_PENDING;
400 lpOvl->InternalHigh = 0;
401 lpOvl->Offset = io_u->offset & 0xFFFFFFFF;
402 lpOvl->OffsetHigh = io_u->offset >> 32;
67897036
BC
403
404 switch (io_u->ddir) {
40c5db35 405 case DDIR_WRITE:
67897036
BC
406 success = WriteFile(io_u->file->hFile, io_u->xfer_buf, io_u->xfer_buflen, &iobytes, lpOvl);
407 break;
408 case DDIR_READ:
409 success = ReadFile(io_u->file->hFile, io_u->xfer_buf, io_u->xfer_buflen, &iobytes, lpOvl);
410 break;
411 case DDIR_SYNC:
412 case DDIR_DATASYNC:
413 case DDIR_SYNC_FILE_RANGE:
414 success = FlushFileBuffers(io_u->file->hFile);
03244c19
BC
415 if (!success) {
416 log_err("windowsaio: failed to flush file buffers\n");
417 io_u->error = win_to_posix_error(GetLastError());
418 }
67897036
BC
419
420 return FIO_Q_COMPLETED;
421 break;
422 case DDIR_TRIM:
03244c19 423 log_err("windowsaio: manual TRIM isn't supported on Windows\n");
67897036
BC
424 io_u->error = 1;
425 io_u->resid = io_u->xfer_buflen;
426 return FIO_Q_COMPLETED;
427 break;
428 default:
429 assert(0);
93bcfd20 430 break;
67897036
BC
431 }
432
40c5db35 433 if (success || GetLastError() == ERROR_IO_PENDING)
67897036 434 rc = FIO_Q_QUEUED;
40c5db35 435 else {
2277d5d5 436 io_u->error = win_to_posix_error(GetLastError());
67897036
BC
437 io_u->resid = io_u->xfer_buflen;
438 }
439
440 return rc;
441}
442
443/* Runs as a thread and waits for queued IO to complete */
444static DWORD WINAPI IoCompletionRoutine(LPVOID lpParameter)
445{
446 OVERLAPPED *ovl;
447 struct fio_overlapped *fov;
448 struct io_u *io_u;
449 struct windowsaio_data *wd;
450 struct thread_ctx *ctx;
451 ULONG_PTR ulKey = 0;
452 DWORD bytes;
453
454 ctx = (struct thread_ctx*)lpParameter;
455 wd = ctx->wd;
456
457 do {
66c098b8 458 if (!GetQueuedCompletionStatus(ctx->iocp, &bytes, &ulKey, &ovl, 250) && ovl == NULL)
67897036
BC
459 continue;
460
461 fov = CONTAINING_RECORD(ovl, struct fio_overlapped, o);
462 io_u = fov->io_u;
463
464 if (ovl->Internal == ERROR_SUCCESS) {
465 io_u->resid = io_u->xfer_buflen - ovl->InternalHigh;
466 io_u->error = 0;
467 } else {
468 io_u->resid = io_u->xfer_buflen;
2277d5d5 469 io_u->error = win_to_posix_error(GetLastError());
67897036
BC
470 }
471
40c5db35 472 fov->io_complete = TRUE;
67897036
BC
473 SetEvent(wd->iocomplete_event);
474 } while (ctx->wd->iothread_running);
475
476 CloseHandle(ctx->iocp);
477 free(ctx);
478 return 0;
479}
480
481static int fio_windowsaio_cancel(struct thread_data *td,
03244c19 482 struct io_u *io_u)
67897036
BC
483{
484 int rc = 0;
485
486 struct windowsaio_data *wd = td->io_ops->data;
487
488 /* If we're running on Vista or newer, we can cancel individual IO requests */
489 if (wd->pCancelIoEx != NULL) {
490 struct fio_overlapped *ovl = io_u->engine_data;
40c5db35 491
03244c19
BC
492 if (!wd->pCancelIoEx(io_u->file->hFile, &ovl->o)) {
493 log_err("windowsaio: failed to cancel io\n");
67897036 494 rc = 1;
03244c19 495 }
67897036
BC
496 } else
497 rc = 1;
498
499 return rc;
500}
501
c73ed246
JA
502static void fio_windowsaio_io_u_free(struct thread_data *td, struct io_u *io_u)
503{
504 struct fio_overlapped *o = io_u->engine_data;
505
506 if (o) {
507 CloseHandle(o->o.hEvent);
508 io_u->engine_data = NULL;
509 free(o);
510 }
511}
512
513static int fio_windowsaio_io_u_init(struct thread_data *td, struct io_u *io_u)
514{
515 struct fio_overlapped *o;
516
517 o = malloc(sizeof(*o));
b0106419 518 o->io_complete = FALSE;
c73ed246
JA
519 o->io_u = io_u;
520 o->o.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
03244c19
BC
521 if (o->o.hEvent == NULL) {
522 log_err("windowsaio: failed to create event handle\n");
c73ed246
JA
523 free(o);
524 return 1;
525 }
526
527 io_u->engine_data = o;
528 return 0;
529}
530
ecc314ba
BC
531static struct ioengine_ops ioengine = {
532 .name = "windowsaio",
533 .version = FIO_IOOPS_VERSION,
534 .init = fio_windowsaio_init,
535 .queue = fio_windowsaio_queue,
536 .cancel = fio_windowsaio_cancel,
537 .getevents = fio_windowsaio_getevents,
538 .event = fio_windowsaio_event,
539 .cleanup = fio_windowsaio_cleanup,
540 .open_file = fio_windowsaio_open_file,
541 .close_file = fio_windowsaio_close_file,
c73ed246
JA
542 .get_file_size = generic_get_file_size,
543 .io_u_init = fio_windowsaio_io_u_init,
544 .io_u_free = fio_windowsaio_io_u_free,
ecc314ba
BC
545};
546
c874d188 547static void fio_init fio_windowsaio_register(void)
ecc314ba
BC
548{
549 register_ioengine(&ioengine);
550}
551
c874d188 552static void fio_exit fio_windowsaio_unregister(void)
ecc314ba
BC
553{
554 unregister_ioengine(&ioengine);
555}