blob: c0d1bd7d44ea5d9433a9e0878a488e686f034e95 [file] [log] [blame]
Bruce Cranecc314b2011-01-04 10:59:30 +01001/*
2 * Native Windows async IO engine
Bruce Cranc874d182013-02-05 00:56:59 +00003 * Copyright (C) 2012-2013 Bruce Cran <bruce@cran.org.uk>
Bruce Cranecc314b2011-01-04 10:59:30 +01004 */
5
Bruce Cranecc314b2011-01-04 10:59:30 +01006#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
Bruce Cranea4500d2011-05-04 07:54:13 -060015typedef BOOL (WINAPI *CANCELIOEX)(HANDLE hFile, LPOVERLAPPED lpOverlapped);
16
Bruce Cran66c098b2012-11-27 12:16:07 +000017int geterrno_from_win_error (DWORD code, int deferrno);
18
Bruce Cran67897032011-05-12 10:06:10 +020019struct fio_overlapped {
20 OVERLAPPED o;
21 struct io_u *io_u;
22 BOOL io_complete;
Bruce Cran67897032011-05-12 10:06:10 +020023};
Bruce Cranecc314b2011-01-04 10:59:30 +010024
Bruce Cran9b836562011-01-08 19:49:54 +010025struct windowsaio_data {
Bruce Cran9b836562011-01-08 19:49:54 +010026 struct io_u **aio_events;
Bruce Cran5a90bb52012-12-06 19:59:42 +000027 HANDLE iocp;
Bruce Cran67897032011-05-12 10:06:10 +020028 HANDLE iothread;
Bruce Cran9b836562011-01-08 19:49:54 +010029 HANDLE iocomplete_event;
Bruce Cranea4500d2011-05-04 07:54:13 -060030 CANCELIOEX pCancelIoEx;
Bruce Cran67897032011-05-12 10:06:10 +020031 BOOL iothread_running;
Bruce Cran9b836562011-01-08 19:49:54 +010032};
33
Bruce Cranecc314b2011-01-04 10:59:30 +010034struct thread_ctx {
Bruce Cran9b836562011-01-08 19:49:54 +010035 HANDLE iocp;
Bruce Cranecc314b2011-01-04 10:59:30 +010036 struct windowsaio_data *wd;
37};
38
Bruce Cranecc314b2011-01-04 10:59:30 +010039static int fio_windowsaio_cancel(struct thread_data *td,
40 struct io_u *io_u);
Bruce Cran67897032011-05-12 10:06:10 +020041static BOOL timeout_expired(DWORD start_count, DWORD end_count);
Bruce Cranecc314b2011-01-04 10:59:30 +010042static int fio_windowsaio_getevents(struct thread_data *td, unsigned int min,
43 unsigned int max, struct timespec *t);
44static struct io_u *fio_windowsaio_event(struct thread_data *td, int event);
45static int fio_windowsaio_queue(struct thread_data *td,
46 struct io_u *io_u);
47static void fio_windowsaio_cleanup(struct thread_data *td);
48static DWORD WINAPI IoCompletionRoutine(LPVOID lpParameter);
49static int fio_windowsaio_init(struct thread_data *td);
50static int fio_windowsaio_open_file(struct thread_data *td, struct fio_file *f);
51static int fio_windowsaio_close_file(struct thread_data fio_unused *td, struct fio_file *f);
Bruce Cran2277d5d2012-12-06 20:33:14 +010052static int win_to_posix_error(DWORD winerr);
Bruce Cran66c098b2012-11-27 12:16:07 +000053
Bruce Cran2277d5d2012-12-06 20:33:14 +010054static int win_to_posix_error(DWORD winerr)
Bruce Cran66c098b2012-11-27 12:16:07 +000055{
56 switch (winerr)
57 {
58 case ERROR_FILE_NOT_FOUND: return ENOENT;
59 case ERROR_PATH_NOT_FOUND: return ENOENT;
60 case ERROR_ACCESS_DENIED: return EACCES;
61 case ERROR_INVALID_HANDLE: return EBADF;
62 case ERROR_NOT_ENOUGH_MEMORY: return ENOMEM;
63 case ERROR_INVALID_DATA: return EINVAL;
64 case ERROR_OUTOFMEMORY: return ENOMEM;
65 case ERROR_INVALID_DRIVE: return ENODEV;
66 case ERROR_NOT_SAME_DEVICE: return EXDEV;
67 case ERROR_WRITE_PROTECT: return EROFS;
68 case ERROR_BAD_UNIT: return ENODEV;
69 case ERROR_SHARING_VIOLATION: return EACCES;
70 case ERROR_LOCK_VIOLATION: return EACCES;
71 case ERROR_SHARING_BUFFER_EXCEEDED: return ENOLCK;
72 case ERROR_HANDLE_DISK_FULL: return ENOSPC;
73 case ERROR_NOT_SUPPORTED: return ENOSYS;
74 case ERROR_FILE_EXISTS: return EEXIST;
75 case ERROR_CANNOT_MAKE: return EPERM;
76 case ERROR_INVALID_PARAMETER: return EINVAL;
77 case ERROR_NO_PROC_SLOTS: return EAGAIN;
78 case ERROR_BROKEN_PIPE: return EPIPE;
79 case ERROR_OPEN_FAILED: return EIO;
80 case ERROR_NO_MORE_SEARCH_HANDLES: return ENFILE;
81 case ERROR_CALL_NOT_IMPLEMENTED: return ENOSYS;
82 case ERROR_INVALID_NAME: return ENOENT;
83 case ERROR_WAIT_NO_CHILDREN: return ECHILD;
84 case ERROR_CHILD_NOT_COMPLETE: return EBUSY;
85 case ERROR_DIR_NOT_EMPTY: return ENOTEMPTY;
86 case ERROR_SIGNAL_REFUSED: return EIO;
87 case ERROR_BAD_PATHNAME: return ENOENT;
88 case ERROR_SIGNAL_PENDING: return EBUSY;
89 case ERROR_MAX_THRDS_REACHED: return EAGAIN;
90 case ERROR_BUSY: return EBUSY;
91 case ERROR_ALREADY_EXISTS: return EEXIST;
92 case ERROR_NO_SIGNAL_SENT: return EIO;
93 case ERROR_FILENAME_EXCED_RANGE: return EINVAL;
94 case ERROR_META_EXPANSION_TOO_LONG: return EINVAL;
95 case ERROR_INVALID_SIGNAL_NUMBER: return EINVAL;
96 case ERROR_THREAD_1_INACTIVE: return EINVAL;
97 case ERROR_BAD_PIPE: return EINVAL;
98 case ERROR_PIPE_BUSY: return EBUSY;
99 case ERROR_NO_DATA: return EPIPE;
100 case ERROR_MORE_DATA: return EAGAIN;
101 case ERROR_DIRECTORY: return ENOTDIR;
102 case ERROR_PIPE_CONNECTED: return EBUSY;
103 case ERROR_NO_TOKEN: return EINVAL;
104 case ERROR_PROCESS_ABORTED: return EFAULT;
105 case ERROR_BAD_DEVICE: return ENODEV;
106 case ERROR_BAD_USERNAME: return EINVAL;
107 case ERROR_OPEN_FILES: return EAGAIN;
108 case ERROR_ACTIVE_CONNECTIONS: return EAGAIN;
109 case ERROR_DEVICE_IN_USE: return EAGAIN;
110 case ERROR_INVALID_AT_INTERRUPT_TIME: return EINTR;
111 case ERROR_IO_DEVICE: return EIO;
112 case ERROR_NOT_OWNER: return EPERM;
113 case ERROR_END_OF_MEDIA: return ENOSPC;
114 case ERROR_EOM_OVERFLOW: return ENOSPC;
115 case ERROR_BEGINNING_OF_MEDIA: return ESPIPE;
116 case ERROR_SETMARK_DETECTED: return ESPIPE;
117 case ERROR_NO_DATA_DETECTED: return ENOSPC;
118 case ERROR_POSSIBLE_DEADLOCK: return EDEADLOCK;
119 case ERROR_CRC: return EIO;
120 case ERROR_NEGATIVE_SEEK: return EINVAL;
121 case ERROR_DISK_FULL: return ENOSPC;
122 case ERROR_NOACCESS: return EFAULT;
123 case ERROR_FILE_INVALID: return ENXIO;
124 }
125
126 return winerr;
127}
Bruce Cranecc314b2011-01-04 10:59:30 +0100128
Bruce Cranecc314b2011-01-04 10:59:30 +0100129static int fio_windowsaio_init(struct thread_data *td)
130{
Bruce Cranecc314b2011-01-04 10:59:30 +0100131 struct windowsaio_data *wd;
Bruce Cranea4500d2011-05-04 07:54:13 -0600132 HANDLE hKernel32Dll;
Bruce Cran9b836562011-01-08 19:49:54 +0100133 int rc = 0;
Bruce Cranecc314b2011-01-04 10:59:30 +0100134
Bruce Cranecc314b2011-01-04 10:59:30 +0100135 wd = malloc(sizeof(struct windowsaio_data));
Bruce Cran9b836562011-01-08 19:49:54 +0100136 if (wd != NULL)
137 ZeroMemory(wd, sizeof(struct windowsaio_data));
138 else
139 rc = 1;
Bruce Cranecc314b2011-01-04 10:59:30 +0100140
Bruce Cran9b836562011-01-08 19:49:54 +0100141 if (!rc) {
142 wd->aio_events = malloc(td->o.iodepth * sizeof(struct io_u*));
143 if (wd->aio_events == NULL)
144 rc = 1;
Bruce Crane4db9fe2011-01-04 14:44:47 +0100145 }
146
Bruce Cran9b836562011-01-08 19:49:54 +0100147 if (!rc) {
Bruce Cran9b836562011-01-08 19:49:54 +0100148 /* Create an auto-reset event */
149 wd->iocomplete_event = CreateEvent(NULL, FALSE, FALSE, NULL);
150 if (wd->iocomplete_event == NULL)
151 rc = 1;
152 }
153
Bruce Cran9b836562011-01-08 19:49:54 +0100154 if (rc) {
Bruce Cran9b836562011-01-08 19:49:54 +0100155 if (wd != NULL) {
Bruce Cran9b836562011-01-08 19:49:54 +0100156 if (wd->aio_events != NULL)
157 free(wd->aio_events);
158
159 free(wd);
160 }
161 }
Bruce Cranecc314b2011-01-04 10:59:30 +0100162
Bruce Cranea4500d2011-05-04 07:54:13 -0600163 hKernel32Dll = GetModuleHandle("kernel32.dll");
Bruce Cran93bcfd22012-02-20 20:18:19 +0100164 wd->pCancelIoEx = (CANCELIOEX)GetProcAddress(hKernel32Dll, "CancelIoEx");
Bruce Cranecc314b2011-01-04 10:59:30 +0100165 td->io_ops->data = wd;
Bruce Cran93bcfd22012-02-20 20:18:19 +0100166
Bruce Cran5a90bb52012-12-06 19:59:42 +0000167
168 if (!rc) {
169 struct thread_ctx *ctx;
170 struct windowsaio_data *wd;
171 HANDLE hFile;
172
173 hFile = CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, 0, 0);
174 if (hFile == INVALID_HANDLE_VALUE)
175 rc = 1;
176
177 wd = td->io_ops->data;
178 wd->iothread_running = TRUE;
179 wd->iocp = hFile;
180
181 if (!rc)
182 ctx = malloc(sizeof(struct thread_ctx));
183
184 if (!rc && ctx == NULL)
185 {
186 log_err("fio: out of memory in windowsaio\n");
187 CloseHandle(hFile);
188 rc = 1;
189 }
190
191 if (!rc)
192 {
193 ctx->iocp = hFile;
194 ctx->wd = wd;
195 wd->iothread = CreateThread(NULL, 0, IoCompletionRoutine, ctx, 0, NULL);
196 }
197
198 if (rc || wd->iothread == NULL)
199 rc = 1;
200 }
201
Bruce Cran4e790982011-07-09 08:20:47 +0200202 return rc;
Bruce Cranecc314b2011-01-04 10:59:30 +0100203}
204
Bruce Cran67897032011-05-12 10:06:10 +0200205static void fio_windowsaio_cleanup(struct thread_data *td)
206{
Bruce Cran67897032011-05-12 10:06:10 +0200207 struct windowsaio_data *wd;
208
209 wd = td->io_ops->data;
210
211 if (wd != NULL) {
Jens Axboe40c5db32012-02-01 23:03:44 +0100212 wd->iothread_running = FALSE;
213 WaitForSingleObject(wd->iothread, INFINITE);
Bruce Cran67897032011-05-12 10:06:10 +0200214
215 CloseHandle(wd->iothread);
216 CloseHandle(wd->iocomplete_event);
217
Bruce Cran67897032011-05-12 10:06:10 +0200218 free(wd->aio_events);
Bruce Cran67897032011-05-12 10:06:10 +0200219 free(wd);
220
221 td->io_ops->data = NULL;
222 }
223}
224
225
Bruce Cranecc314b2011-01-04 10:59:30 +0100226static int fio_windowsaio_open_file(struct thread_data *td, struct fio_file *f)
227{
228 int rc = 0;
Bruce Cran4e790982011-07-09 08:20:47 +0200229 DWORD flags = FILE_FLAG_POSIX_SEMANTICS | FILE_FLAG_OVERLAPPED;
Bruce Cranecc314b2011-01-04 10:59:30 +0100230 DWORD sharemode = FILE_SHARE_READ | FILE_SHARE_WRITE;
231 DWORD openmode = OPEN_ALWAYS;
232 DWORD access;
233
234 dprint(FD_FILE, "fd open %s\n", f->file_name);
235
Bruce Cranecc314b2011-01-04 10:59:30 +0100236 if (f->filetype == FIO_TYPE_PIPE) {
237 log_err("fio: windowsaio doesn't support pipes\n");
238 return 1;
239 }
240
241 if (!strcmp(f->file_name, "-")) {
242 log_err("fio: can't read/write to stdin/out\n");
243 return 1;
244 }
245
246 if (td->o.odirect)
247 flags |= FILE_FLAG_NO_BUFFERING;
248 if (td->o.sync_io)
249 flags |= FILE_FLAG_WRITE_THROUGH;
250
Bruce Cran93bcfd22012-02-20 20:18:19 +0100251 /*
252 * Inform Windows whether we're going to be doing sequential or
253 * random io so it can tune the Cache Manager
254 */
Bruce Cranecc314b2011-01-04 10:59:30 +0100255 if (td->o.td_ddir == TD_DDIR_READ ||
Bruce Cranea4500d2011-05-04 07:54:13 -0600256 td->o.td_ddir == TD_DDIR_WRITE)
Bruce Cranecc314b2011-01-04 10:59:30 +0100257 flags |= FILE_FLAG_SEQUENTIAL_SCAN;
Bruce Cranecc314b2011-01-04 10:59:30 +0100258 else
Bruce Cranecc314b2011-01-04 10:59:30 +0100259 flags |= FILE_FLAG_RANDOM_ACCESS;
Bruce Cranecc314b2011-01-04 10:59:30 +0100260
Bruce Cranea4500d2011-05-04 07:54:13 -0600261 if (!td_write(td) || read_only)
Bruce Cranecc314b2011-01-04 10:59:30 +0100262 access = GENERIC_READ;
263 else
264 access = (GENERIC_READ | GENERIC_WRITE);
265
Bruce Cran93bcfd22012-02-20 20:18:19 +0100266 if (td->o.create_on_open)
Bruce Cranecc314b2011-01-04 10:59:30 +0100267 openmode = OPEN_ALWAYS;
268 else
269 openmode = OPEN_EXISTING;
270
271 f->hFile = CreateFile(f->file_name, access, sharemode,
272 NULL, openmode, flags, NULL);
273
Bruce Cran4e790982011-07-09 08:20:47 +0200274 if (f->hFile == INVALID_HANDLE_VALUE)
Bruce Cranecc314b2011-01-04 10:59:30 +0100275 rc = 1;
Bruce Cranecc314b2011-01-04 10:59:30 +0100276
Bruce Cran93bcfd22012-02-20 20:18:19 +0100277 /* Only set up the completion port and thread if we're not just
Bruce Cranecc314b2011-01-04 10:59:30 +0100278 * querying the device size */
Jens Axboe40c5db32012-02-01 23:03:44 +0100279 if (!rc && td->io_ops->data != NULL) {
Jens Axboe40c5db32012-02-01 23:03:44 +0100280 struct windowsaio_data *wd;
281
Jens Axboe40c5db32012-02-01 23:03:44 +0100282 wd = td->io_ops->data;
Bruce Cran9b836562011-01-08 19:49:54 +0100283
Bruce Cran5a90bb52012-12-06 19:59:42 +0000284 if (CreateIoCompletionPort(f->hFile, wd->iocp, 0, 0) == NULL)
Bruce Cranecc314b2011-01-04 10:59:30 +0100285 rc = 1;
Bruce Cranecc314b2011-01-04 10:59:30 +0100286 }
287
Bruce Cranecc314b2011-01-04 10:59:30 +0100288 return rc;
289}
290
291static int fio_windowsaio_close_file(struct thread_data fio_unused *td, struct fio_file *f)
292{
Bruce Cran4e790982011-07-09 08:20:47 +0200293 int rc = 0;
Bruce Cran1e42cc32012-02-01 21:46:55 +0100294
Bruce Cran9b836562011-01-08 19:49:54 +0100295 dprint(FD_FILE, "fd close %s\n", f->file_name);
296
Bruce Cranecc314b2011-01-04 10:59:30 +0100297 if (f->hFile != INVALID_HANDLE_VALUE) {
Bruce Cran9b836562011-01-08 19:49:54 +0100298 if (!CloseHandle(f->hFile))
Bruce Cran4e790982011-07-09 08:20:47 +0200299 rc = 1;
Bruce Cranecc314b2011-01-04 10:59:30 +0100300 }
301
302 f->hFile = INVALID_HANDLE_VALUE;
Bruce Cran4e790982011-07-09 08:20:47 +0200303 return rc;
Bruce Cranecc314b2011-01-04 10:59:30 +0100304}
305
Bruce Cran67897032011-05-12 10:06:10 +0200306static BOOL timeout_expired(DWORD start_count, DWORD end_count)
307{
308 BOOL expired = FALSE;
309 DWORD current_time;
310
311 current_time = GetTickCount();
312
313 if ((end_count > start_count) && current_time >= end_count)
314 expired = TRUE;
315 else if (current_time < start_count && current_time > end_count)
316 expired = TRUE;
317
318 return expired;
319}
320
321static struct io_u* fio_windowsaio_event(struct thread_data *td, int event)
322{
323 struct windowsaio_data *wd = td->io_ops->data;
324 return wd->aio_events[event];
325}
326
327static int fio_windowsaio_getevents(struct thread_data *td, unsigned int min,
328 unsigned int max, struct timespec *t)
329{
330 struct windowsaio_data *wd = td->io_ops->data;
331 struct flist_head *entry;
332 unsigned int dequeued = 0;
333 struct io_u *io_u;
334 struct fio_overlapped *fov;
335 DWORD start_count = 0;
336 DWORD end_count = 0;
337 DWORD status;
338 DWORD mswait = 250;
339
340 if (t != NULL) {
341 mswait = (t->tv_sec * 1000) + (t->tv_nsec / 1000000);
342 start_count = GetTickCount();
343 end_count = start_count + (t->tv_sec * 1000) + (t->tv_nsec / 1000000);
344 }
345
346 do {
347 flist_for_each(entry, &td->io_u_busylist) {
348 io_u = flist_entry(entry, struct io_u, list);
349 fov = (struct fio_overlapped*)io_u->engine_data;
350
351 if (fov->io_complete) {
Jens Axboe40c5db32012-02-01 23:03:44 +0100352 fov->io_complete = FALSE;
Bruce Cranf9a58c22012-04-04 08:35:13 -0600353 ResetEvent(fov->o.hEvent);
Bruce Cran67897032011-05-12 10:06:10 +0200354 wd->aio_events[dequeued] = io_u;
355 dequeued++;
356 }
357
358 if (dequeued >= min)
359 break;
360 }
361
Jens Axboe40c5db32012-02-01 23:03:44 +0100362 if (dequeued < min) {
Bruce Cran67897032011-05-12 10:06:10 +0200363 status = WaitForSingleObject(wd->iocomplete_event, mswait);
Bruce Cranf9a58c22012-04-04 08:35:13 -0600364 if (status != WAIT_OBJECT_0 && dequeued >= min)
Bruce Cran67897032011-05-12 10:06:10 +0200365 break;
366 }
367
368 if (dequeued >= min || (t != NULL && timeout_expired(start_count, end_count)))
369 break;
370 } while (1);
371
372 return dequeued;
373}
374
Jens Axboe40c5db32012-02-01 23:03:44 +0100375static int fio_windowsaio_queue(struct thread_data *td, struct io_u *io_u)
Bruce Cran67897032011-05-12 10:06:10 +0200376{
Jens Axboec73ed242012-12-06 20:30:38 +0100377 struct fio_overlapped *o = io_u->engine_data;
378 LPOVERLAPPED lpOvl = &o->o;
Bruce Cran67897032011-05-12 10:06:10 +0200379 DWORD iobytes;
Bruce Cran93bcfd22012-02-20 20:18:19 +0100380 BOOL success = FALSE;
Bruce Cran67897032011-05-12 10:06:10 +0200381 int rc = FIO_Q_COMPLETED;
382
383 fio_ro_check(td, io_u);
384
Bruce Cran4e790982011-07-09 08:20:47 +0200385 lpOvl->Internal = STATUS_PENDING;
386 lpOvl->InternalHigh = 0;
387 lpOvl->Offset = io_u->offset & 0xFFFFFFFF;
388 lpOvl->OffsetHigh = io_u->offset >> 32;
Bruce Cran67897032011-05-12 10:06:10 +0200389
390 switch (io_u->ddir) {
Jens Axboe40c5db32012-02-01 23:03:44 +0100391 case DDIR_WRITE:
Bruce Cran67897032011-05-12 10:06:10 +0200392 success = WriteFile(io_u->file->hFile, io_u->xfer_buf, io_u->xfer_buflen, &iobytes, lpOvl);
393 break;
394 case DDIR_READ:
395 success = ReadFile(io_u->file->hFile, io_u->xfer_buf, io_u->xfer_buflen, &iobytes, lpOvl);
396 break;
397 case DDIR_SYNC:
398 case DDIR_DATASYNC:
399 case DDIR_SYNC_FILE_RANGE:
400 success = FlushFileBuffers(io_u->file->hFile);
401 if (!success)
Bruce Cran2277d5d2012-12-06 20:33:14 +0100402 io_u->error = win_to_posix_error(GetLastError());
Bruce Cran67897032011-05-12 10:06:10 +0200403
404 return FIO_Q_COMPLETED;
405 break;
406 case DDIR_TRIM:
407 log_err("manual TRIM isn't supported on Windows");
408 io_u->error = 1;
409 io_u->resid = io_u->xfer_buflen;
410 return FIO_Q_COMPLETED;
411 break;
412 default:
413 assert(0);
Bruce Cran93bcfd22012-02-20 20:18:19 +0100414 break;
Bruce Cran67897032011-05-12 10:06:10 +0200415 }
416
Jens Axboe40c5db32012-02-01 23:03:44 +0100417 if (success || GetLastError() == ERROR_IO_PENDING)
Bruce Cran67897032011-05-12 10:06:10 +0200418 rc = FIO_Q_QUEUED;
Jens Axboe40c5db32012-02-01 23:03:44 +0100419 else {
Bruce Cran2277d5d2012-12-06 20:33:14 +0100420 io_u->error = win_to_posix_error(GetLastError());
Bruce Cran67897032011-05-12 10:06:10 +0200421 io_u->resid = io_u->xfer_buflen;
422 }
423
424 return rc;
425}
426
427/* Runs as a thread and waits for queued IO to complete */
428static DWORD WINAPI IoCompletionRoutine(LPVOID lpParameter)
429{
430 OVERLAPPED *ovl;
431 struct fio_overlapped *fov;
432 struct io_u *io_u;
433 struct windowsaio_data *wd;
434 struct thread_ctx *ctx;
435 ULONG_PTR ulKey = 0;
436 DWORD bytes;
437
438 ctx = (struct thread_ctx*)lpParameter;
439 wd = ctx->wd;
440
441 do {
Bruce Cran66c098b2012-11-27 12:16:07 +0000442 if (!GetQueuedCompletionStatus(ctx->iocp, &bytes, &ulKey, &ovl, 250) && ovl == NULL)
Bruce Cran67897032011-05-12 10:06:10 +0200443 continue;
444
445 fov = CONTAINING_RECORD(ovl, struct fio_overlapped, o);
446 io_u = fov->io_u;
447
448 if (ovl->Internal == ERROR_SUCCESS) {
449 io_u->resid = io_u->xfer_buflen - ovl->InternalHigh;
450 io_u->error = 0;
451 } else {
452 io_u->resid = io_u->xfer_buflen;
Bruce Cran2277d5d2012-12-06 20:33:14 +0100453 io_u->error = win_to_posix_error(GetLastError());
Bruce Cran67897032011-05-12 10:06:10 +0200454 }
455
Jens Axboe40c5db32012-02-01 23:03:44 +0100456 fov->io_complete = TRUE;
Bruce Cran67897032011-05-12 10:06:10 +0200457 SetEvent(wd->iocomplete_event);
458 } while (ctx->wd->iothread_running);
459
460 CloseHandle(ctx->iocp);
461 free(ctx);
462 return 0;
463}
464
465static int fio_windowsaio_cancel(struct thread_data *td,
466 struct io_u *io_u)
467{
468 int rc = 0;
469
470 struct windowsaio_data *wd = td->io_ops->data;
471
472 /* If we're running on Vista or newer, we can cancel individual IO requests */
473 if (wd->pCancelIoEx != NULL) {
474 struct fio_overlapped *ovl = io_u->engine_data;
Jens Axboe40c5db32012-02-01 23:03:44 +0100475
Bruce Cran67897032011-05-12 10:06:10 +0200476 if (!wd->pCancelIoEx(io_u->file->hFile, &ovl->o))
477 rc = 1;
478 } else
479 rc = 1;
480
481 return rc;
482}
483
Jens Axboec73ed242012-12-06 20:30:38 +0100484static void fio_windowsaio_io_u_free(struct thread_data *td, struct io_u *io_u)
485{
486 struct fio_overlapped *o = io_u->engine_data;
487
488 if (o) {
489 CloseHandle(o->o.hEvent);
490 io_u->engine_data = NULL;
491 free(o);
492 }
493}
494
495static int fio_windowsaio_io_u_init(struct thread_data *td, struct io_u *io_u)
496{
497 struct fio_overlapped *o;
498
499 o = malloc(sizeof(*o));
Jens Axboeb0106412012-12-18 11:53:46 +0100500 o->io_complete = FALSE;
Jens Axboec73ed242012-12-06 20:30:38 +0100501 o->io_u = io_u;
502 o->o.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
503 if (!o->o.hEvent) {
504 free(o);
505 return 1;
506 }
507
508 io_u->engine_data = o;
509 return 0;
510}
511
Bruce Cranecc314b2011-01-04 10:59:30 +0100512static struct ioengine_ops ioengine = {
513 .name = "windowsaio",
514 .version = FIO_IOOPS_VERSION,
515 .init = fio_windowsaio_init,
516 .queue = fio_windowsaio_queue,
517 .cancel = fio_windowsaio_cancel,
518 .getevents = fio_windowsaio_getevents,
519 .event = fio_windowsaio_event,
520 .cleanup = fio_windowsaio_cleanup,
521 .open_file = fio_windowsaio_open_file,
522 .close_file = fio_windowsaio_close_file,
Jens Axboec73ed242012-12-06 20:30:38 +0100523 .get_file_size = generic_get_file_size,
524 .io_u_init = fio_windowsaio_io_u_init,
525 .io_u_free = fio_windowsaio_io_u_free,
Bruce Cranecc314b2011-01-04 10:59:30 +0100526};
527
Bruce Cranc874d182013-02-05 00:56:59 +0000528static void fio_init fio_windowsaio_register(void)
Bruce Cranecc314b2011-01-04 10:59:30 +0100529{
530 register_ioengine(&ioengine);
531}
532
Bruce Cranc874d182013-02-05 00:56:59 +0000533static void fio_exit fio_windowsaio_unregister(void)
Bruce Cranecc314b2011-01-04 10:59:30 +0100534{
535 unregister_ioengine(&ioengine);
536}