net: fix segfault with receiver, tcp, and no hostname set
[fio.git] / engines / windowsaio.c
... / ...
CommitLineData
1/*
2 * Native Windows async IO engine
3 * Copyright (C) 2012 Bruce Cran <bruce@cran.org.uk>
4 */
5
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
15typedef BOOL (WINAPI *CANCELIOEX)(HANDLE hFile, LPOVERLAPPED lpOverlapped);
16
17int geterrno_from_win_error (DWORD code, int deferrno);
18
19struct fio_overlapped {
20 OVERLAPPED o;
21 struct io_u *io_u;
22 BOOL io_complete;
23 BOOL io_free;
24};
25
26struct windowsaio_data {
27 struct fio_overlapped *ovls;
28 struct io_u **aio_events;
29 HANDLE iothread;
30 HANDLE iocomplete_event;
31 CANCELIOEX pCancelIoEx;
32 BOOL iothread_running;
33};
34
35struct thread_ctx {
36 HANDLE iocp;
37 struct windowsaio_data *wd;
38};
39
40static int fio_windowsaio_cancel(struct thread_data *td,
41 struct io_u *io_u);
42static BOOL timeout_expired(DWORD start_count, DWORD end_count);
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);
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}
129
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
137static int fio_windowsaio_init(struct thread_data *td)
138{
139 struct windowsaio_data *wd;
140 HANDLE hKernel32Dll;
141 int rc = 0;
142 int i;
143
144 wd = malloc(sizeof(struct windowsaio_data));
145 if (wd != NULL)
146 ZeroMemory(wd, sizeof(struct windowsaio_data));
147 else
148 rc = 1;
149
150 if (!rc) {
151 wd->aio_events = malloc(td->o.iodepth * sizeof(struct io_u*));
152 if (wd->aio_events == NULL)
153 rc = 1;
154 }
155
156 if (!rc) {
157 wd->ovls = malloc(td->o.iodepth * sizeof(struct fio_overlapped));
158 if (wd->ovls == NULL)
159 rc = 1;
160 }
161
162 if (!rc) {
163 for (i = 0; i < td->o.iodepth; i++) {
164 wd->ovls[i].io_free = TRUE;
165 wd->ovls[i].io_complete = FALSE;
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 }
172 }
173 }
174
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
182 if (rc) {
183 if (wd != NULL) {
184 if (wd->ovls != NULL)
185 free(wd->ovls);
186 if (wd->aio_events != NULL)
187 free(wd->aio_events);
188
189 free(wd);
190 }
191 }
192
193 hKernel32Dll = GetModuleHandle("kernel32.dll");
194 wd->pCancelIoEx = (CANCELIOEX)GetProcAddress(hKernel32Dll, "CancelIoEx");
195 td->io_ops->data = wd;
196
197 return rc;
198}
199
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) {
208 wd->iothread_running = FALSE;
209 WaitForSingleObject(wd->iothread, INFINITE);
210
211 CloseHandle(wd->iothread);
212 CloseHandle(wd->iocomplete_event);
213
214 for (i = 0; i < td->o.iodepth; i++) {
215 CloseHandle(wd->ovls[i].o.hEvent);
216 }
217
218 free(wd->aio_events);
219 free(wd->ovls);
220 free(wd);
221
222 td->io_ops->data = NULL;
223 }
224}
225
226
227static int fio_windowsaio_open_file(struct thread_data *td, struct fio_file *f)
228{
229 int rc = 0;
230 HANDLE hFile;
231 DWORD flags = FILE_FLAG_POSIX_SEMANTICS | FILE_FLAG_OVERLAPPED;
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
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
253 /*
254 * Inform Windows whether we're going to be doing sequential or
255 * random io so it can tune the Cache Manager
256 */
257 if (td->o.td_ddir == TD_DDIR_READ ||
258 td->o.td_ddir == TD_DDIR_WRITE)
259 flags |= FILE_FLAG_SEQUENTIAL_SCAN;
260 else
261 flags |= FILE_FLAG_RANDOM_ACCESS;
262
263 if (!td_write(td) || read_only)
264 access = GENERIC_READ;
265 else
266 access = (GENERIC_READ | GENERIC_WRITE);
267
268 if (td->o.create_on_open)
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
276 if (f->hFile == INVALID_HANDLE_VALUE)
277 rc = 1;
278
279 /* Only set up the completion port and thread if we're not just
280 * querying the device size */
281 if (!rc && td->io_ops->data != NULL) {
282 struct thread_ctx *ctx;
283 struct windowsaio_data *wd;
284
285 hFile = CreateIoCompletionPort(f->hFile, NULL, 0, 0);
286
287 wd = td->io_ops->data;
288 wd->iothread_running = TRUE;
289
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 }
297
298 if (rc || wd->iothread == NULL)
299 rc = 1;
300 }
301
302 return rc;
303}
304
305static int fio_windowsaio_close_file(struct thread_data fio_unused *td, struct fio_file *f)
306{
307 int rc = 0;
308
309 dprint(FD_FILE, "fd close %s\n", f->file_name);
310
311 if (f->hFile != INVALID_HANDLE_VALUE) {
312 if (!CloseHandle(f->hFile))
313 rc = 1;
314 }
315
316 f->hFile = INVALID_HANDLE_VALUE;
317 return rc;
318}
319
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) {
366 fov->io_complete = FALSE;
367 fov->io_free = TRUE;
368 ResetEvent(fov->o.hEvent);
369 wd->aio_events[dequeued] = io_u;
370 dequeued++;
371 }
372
373 if (dequeued >= min)
374 break;
375 }
376
377 if (dequeued < min) {
378 status = WaitForSingleObject(wd->iocomplete_event, mswait);
379 if (status != WAIT_OBJECT_0 && dequeued >= min)
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
390static int fio_windowsaio_queue(struct thread_data *td, struct io_u *io_u)
391{
392 LPOVERLAPPED lpOvl = NULL;
393 struct windowsaio_data *wd;
394 DWORD iobytes;
395 BOOL success = FALSE;
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
403 for (index = 0; index < td->o.iodepth; index++) {
404 if (wd->ovls[index].io_free)
405 break;
406 }
407
408 assert(index < td->o.iodepth);
409
410 wd->ovls[index].io_free = FALSE;
411 wd->ovls[index].io_u = io_u;
412 lpOvl = &wd->ovls[index].o;
413 lpOvl->Internal = STATUS_PENDING;
414 lpOvl->InternalHigh = 0;
415 lpOvl->Offset = io_u->offset & 0xFFFFFFFF;
416 lpOvl->OffsetHigh = io_u->offset >> 32;
417 io_u->engine_data = &wd->ovls[index];
418
419 switch (io_u->ddir) {
420 case DDIR_WRITE:
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)
431 io_u->error = win_to_poxix_error(GetLastError());
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);
443 break;
444 }
445
446 if (success || GetLastError() == ERROR_IO_PENDING)
447 rc = FIO_Q_QUEUED;
448 else {
449 io_u->error = win_to_poxix_error(GetLastError());
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 {
471 if (!GetQueuedCompletionStatus(ctx->iocp, &bytes, &ulKey, &ovl, 250) && ovl == NULL)
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;
482 io_u->error = win_to_poxix_error(GetLastError());
483 }
484
485 fov->io_complete = TRUE;
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;
504
505 if (!wd->pCancelIoEx(io_u->file->hFile, &ovl->o))
506 rc = 1;
507 } else
508 rc = 1;
509
510 return rc;
511}
512
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}