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