1 | /* $Id: fileaio-linux.cpp 57358 2015-08-14 15:16:38Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - File async I/O, native implementation for the Linux host platform.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2015 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | */
|
---|
26 |
|
---|
27 | /** @page pg_rtfileaio_linux RTFile Async I/O - Linux Implementation Notes
|
---|
28 | * @internal
|
---|
29 | *
|
---|
30 | * Linux implements the kernel async I/O API through the io_* syscalls. They are
|
---|
31 | * not exposed in the glibc (the aio_* API uses userspace threads and blocking
|
---|
32 | * I/O operations to simulate async behavior). There is an external library
|
---|
33 | * called libaio which implements these syscalls but because we don't want to
|
---|
34 | * have another dependency and this library is not installed by default and the
|
---|
35 | * interface is really simple we use the kernel interface directly using wrapper
|
---|
36 | * functions.
|
---|
37 | *
|
---|
38 | * The interface has some limitations. The first one is that the file must be
|
---|
39 | * opened with O_DIRECT. This disables caching done by the kernel which can be
|
---|
40 | * compensated if the user of this API implements caching itself. The next
|
---|
41 | * limitation is that data buffers must be aligned at a 512 byte boundary or the
|
---|
42 | * request will fail.
|
---|
43 | */
|
---|
44 | /** @todo r=bird: What's this about "must be opened with O_DIRECT"? An
|
---|
45 | * explanation would be nice, esp. seeing what Linus is quoted saying
|
---|
46 | * about it in the open man page... */
|
---|
47 |
|
---|
48 |
|
---|
49 | /*********************************************************************************************************************************
|
---|
50 | * Header Files *
|
---|
51 | *********************************************************************************************************************************/
|
---|
52 | #define LOG_GROUP RTLOGGROUP_FILE
|
---|
53 | #include <iprt/asm.h>
|
---|
54 | #include <iprt/mem.h>
|
---|
55 | #include <iprt/assert.h>
|
---|
56 | #include <iprt/string.h>
|
---|
57 | #include <iprt/err.h>
|
---|
58 | #include <iprt/log.h>
|
---|
59 | #include <iprt/thread.h>
|
---|
60 | #include "internal/fileaio.h"
|
---|
61 |
|
---|
62 | #include <unistd.h>
|
---|
63 | #include <sys/syscall.h>
|
---|
64 | #include <errno.h>
|
---|
65 |
|
---|
66 | #include <iprt/file.h>
|
---|
67 |
|
---|
68 |
|
---|
69 | /*********************************************************************************************************************************
|
---|
70 | * Structures and Typedefs *
|
---|
71 | *********************************************************************************************************************************/
|
---|
72 | /** The async I/O context handle */
|
---|
73 | typedef unsigned long LNXKAIOCONTEXT;
|
---|
74 |
|
---|
75 | /**
|
---|
76 | * Supported commands for the iocbs
|
---|
77 | */
|
---|
78 | enum
|
---|
79 | {
|
---|
80 | LNXKAIO_IOCB_CMD_READ = 0,
|
---|
81 | LNXKAIO_IOCB_CMD_WRITE = 1,
|
---|
82 | LNXKAIO_IOCB_CMD_FSYNC = 2,
|
---|
83 | LNXKAIO_IOCB_CMD_FDSYNC = 3
|
---|
84 | };
|
---|
85 |
|
---|
86 | /**
|
---|
87 | * The iocb structure of a request which is passed to the kernel.
|
---|
88 | *
|
---|
89 | * We redefined this here because the version in the header lacks padding
|
---|
90 | * for 32bit.
|
---|
91 | */
|
---|
92 | typedef struct LNXKAIOIOCB
|
---|
93 | {
|
---|
94 | /** Opaque pointer to data which is returned on an I/O event. */
|
---|
95 | void *pvUser;
|
---|
96 | #ifdef RT_ARCH_X86
|
---|
97 | uint32_t u32Padding0;
|
---|
98 | #endif
|
---|
99 | /** Contains the request number and is set by the kernel. */
|
---|
100 | uint32_t u32Key;
|
---|
101 | /** Reserved. */
|
---|
102 | uint32_t u32Reserved0;
|
---|
103 | /** The I/O opcode. */
|
---|
104 | uint16_t u16IoOpCode;
|
---|
105 | /** Request priority. */
|
---|
106 | int16_t i16Priority;
|
---|
107 | /** The file descriptor. */
|
---|
108 | uint32_t uFileDesc;
|
---|
109 | /** The userspace pointer to the buffer containing/receiving the data. */
|
---|
110 | void *pvBuf;
|
---|
111 | #ifdef RT_ARCH_X86
|
---|
112 | uint32_t u32Padding1;
|
---|
113 | #endif
|
---|
114 | /** How many bytes to transfer. */
|
---|
115 | #ifdef RT_ARCH_X86
|
---|
116 | uint32_t cbTransfer;
|
---|
117 | uint32_t u32Padding2;
|
---|
118 | #elif defined(RT_ARCH_AMD64)
|
---|
119 | uint64_t cbTransfer;
|
---|
120 | #else
|
---|
121 | # error "Unknown architecture"
|
---|
122 | #endif
|
---|
123 | /** At which offset to start the transfer. */
|
---|
124 | int64_t off;
|
---|
125 | /** Reserved. */
|
---|
126 | uint64_t u64Reserved1;
|
---|
127 | /** Flags */
|
---|
128 | uint32_t fFlags;
|
---|
129 | /** Readyness signal file descriptor. */
|
---|
130 | uint32_t u32ResFd;
|
---|
131 | } LNXKAIOIOCB, *PLNXKAIOIOCB;
|
---|
132 |
|
---|
133 | /**
|
---|
134 | * I/O event structure to notify about completed requests.
|
---|
135 | * Redefined here too because of the padding.
|
---|
136 | */
|
---|
137 | typedef struct LNXKAIOIOEVENT
|
---|
138 | {
|
---|
139 | /** The pvUser field from the iocb. */
|
---|
140 | void *pvUser;
|
---|
141 | #ifdef RT_ARCH_X86
|
---|
142 | uint32_t u32Padding0;
|
---|
143 | #endif
|
---|
144 | /** The LNXKAIOIOCB object this event is for. */
|
---|
145 | PLNXKAIOIOCB *pIoCB;
|
---|
146 | #ifdef RT_ARCH_X86
|
---|
147 | uint32_t u32Padding1;
|
---|
148 | #endif
|
---|
149 | /** The result code of the operation .*/
|
---|
150 | #ifdef RT_ARCH_X86
|
---|
151 | int32_t rc;
|
---|
152 | uint32_t u32Padding2;
|
---|
153 | #elif defined(RT_ARCH_AMD64)
|
---|
154 | int64_t rc;
|
---|
155 | #else
|
---|
156 | # error "Unknown architecture"
|
---|
157 | #endif
|
---|
158 | /** Secondary result code. */
|
---|
159 | #ifdef RT_ARCH_X86
|
---|
160 | int32_t rc2;
|
---|
161 | uint32_t u32Padding3;
|
---|
162 | #elif defined(RT_ARCH_AMD64)
|
---|
163 | int64_t rc2;
|
---|
164 | #else
|
---|
165 | # error "Unknown architecture"
|
---|
166 | #endif
|
---|
167 | } LNXKAIOIOEVENT, *PLNXKAIOIOEVENT;
|
---|
168 |
|
---|
169 |
|
---|
170 | /**
|
---|
171 | * Async I/O completion context state.
|
---|
172 | */
|
---|
173 | typedef struct RTFILEAIOCTXINTERNAL
|
---|
174 | {
|
---|
175 | /** Handle to the async I/O context. */
|
---|
176 | LNXKAIOCONTEXT AioContext;
|
---|
177 | /** Maximum number of requests this context can handle. */
|
---|
178 | int cRequestsMax;
|
---|
179 | /** Current number of requests active on this context. */
|
---|
180 | volatile int32_t cRequests;
|
---|
181 | /** The ID of the thread which is currently waiting for requests. */
|
---|
182 | volatile RTTHREAD hThreadWait;
|
---|
183 | /** Flag whether the thread was woken up. */
|
---|
184 | volatile bool fWokenUp;
|
---|
185 | /** Flag whether the thread is currently waiting in the syscall. */
|
---|
186 | volatile bool fWaiting;
|
---|
187 | /** Flags given during creation. */
|
---|
188 | uint32_t fFlags;
|
---|
189 | /** Magic value (RTFILEAIOCTX_MAGIC). */
|
---|
190 | uint32_t u32Magic;
|
---|
191 | } RTFILEAIOCTXINTERNAL;
|
---|
192 | /** Pointer to an internal context structure. */
|
---|
193 | typedef RTFILEAIOCTXINTERNAL *PRTFILEAIOCTXINTERNAL;
|
---|
194 |
|
---|
195 | /**
|
---|
196 | * Async I/O request state.
|
---|
197 | */
|
---|
198 | typedef struct RTFILEAIOREQINTERNAL
|
---|
199 | {
|
---|
200 | /** The aio control block. This must be the FIRST elment in
|
---|
201 | * the structure! (see notes below) */
|
---|
202 | LNXKAIOIOCB AioCB;
|
---|
203 | /** Current state the request is in. */
|
---|
204 | RTFILEAIOREQSTATE enmState;
|
---|
205 | /** The I/O context this request is associated with. */
|
---|
206 | LNXKAIOCONTEXT AioContext;
|
---|
207 | /** Return code the request completed with. */
|
---|
208 | int Rc;
|
---|
209 | /** Number of bytes actually transferred. */
|
---|
210 | size_t cbTransfered;
|
---|
211 | /** Completion context we are assigned to. */
|
---|
212 | PRTFILEAIOCTXINTERNAL pCtxInt;
|
---|
213 | /** Magic value (RTFILEAIOREQ_MAGIC). */
|
---|
214 | uint32_t u32Magic;
|
---|
215 | } RTFILEAIOREQINTERNAL;
|
---|
216 | /** Pointer to an internal request structure. */
|
---|
217 | typedef RTFILEAIOREQINTERNAL *PRTFILEAIOREQINTERNAL;
|
---|
218 |
|
---|
219 |
|
---|
220 | /*********************************************************************************************************************************
|
---|
221 | * Defined Constants And Macros *
|
---|
222 | *********************************************************************************************************************************/
|
---|
223 | /** The max number of events to get in one call. */
|
---|
224 | #define AIO_MAXIMUM_REQUESTS_PER_CONTEXT 64
|
---|
225 |
|
---|
226 |
|
---|
227 | /**
|
---|
228 | * Creates a new async I/O context.
|
---|
229 | */
|
---|
230 | DECLINLINE(int) rtFileAsyncIoLinuxCreate(unsigned cEvents, LNXKAIOCONTEXT *pAioContext)
|
---|
231 | {
|
---|
232 | int rc = syscall(__NR_io_setup, cEvents, pAioContext);
|
---|
233 | if (RT_UNLIKELY(rc == -1))
|
---|
234 | {
|
---|
235 | if (errno == EAGAIN)
|
---|
236 | return VERR_FILE_AIO_INSUFFICIENT_EVENTS;
|
---|
237 | else
|
---|
238 | return RTErrConvertFromErrno(errno);
|
---|
239 | }
|
---|
240 |
|
---|
241 | return VINF_SUCCESS;
|
---|
242 | }
|
---|
243 |
|
---|
244 | /**
|
---|
245 | * Destroys a async I/O context.
|
---|
246 | */
|
---|
247 | DECLINLINE(int) rtFileAsyncIoLinuxDestroy(LNXKAIOCONTEXT AioContext)
|
---|
248 | {
|
---|
249 | int rc = syscall(__NR_io_destroy, AioContext);
|
---|
250 | if (RT_UNLIKELY(rc == -1))
|
---|
251 | return RTErrConvertFromErrno(errno);
|
---|
252 |
|
---|
253 | return VINF_SUCCESS;
|
---|
254 | }
|
---|
255 |
|
---|
256 | /**
|
---|
257 | * Submits an array of I/O requests to the kernel.
|
---|
258 | */
|
---|
259 | DECLINLINE(int) rtFileAsyncIoLinuxSubmit(LNXKAIOCONTEXT AioContext, long cReqs, LNXKAIOIOCB **ppIoCB, int *pcSubmitted)
|
---|
260 | {
|
---|
261 | int rc = syscall(__NR_io_submit, AioContext, cReqs, ppIoCB);
|
---|
262 | if (RT_UNLIKELY(rc == -1))
|
---|
263 | return RTErrConvertFromErrno(errno);
|
---|
264 |
|
---|
265 | *pcSubmitted = rc;
|
---|
266 |
|
---|
267 | return VINF_SUCCESS;
|
---|
268 | }
|
---|
269 |
|
---|
270 | /**
|
---|
271 | * Cancels a I/O request.
|
---|
272 | */
|
---|
273 | DECLINLINE(int) rtFileAsyncIoLinuxCancel(LNXKAIOCONTEXT AioContext, PLNXKAIOIOCB pIoCB, PLNXKAIOIOEVENT pIoResult)
|
---|
274 | {
|
---|
275 | int rc = syscall(__NR_io_cancel, AioContext, pIoCB, pIoResult);
|
---|
276 | if (RT_UNLIKELY(rc == -1))
|
---|
277 | return RTErrConvertFromErrno(errno);
|
---|
278 |
|
---|
279 | return VINF_SUCCESS;
|
---|
280 | }
|
---|
281 |
|
---|
282 | /**
|
---|
283 | * Waits for I/O events.
|
---|
284 | * @returns Number of events (natural number w/ 0), IPRT error code (negative).
|
---|
285 | */
|
---|
286 | DECLINLINE(int) rtFileAsyncIoLinuxGetEvents(LNXKAIOCONTEXT AioContext, long cReqsMin, long cReqs,
|
---|
287 | PLNXKAIOIOEVENT paIoResults, struct timespec *pTimeout)
|
---|
288 | {
|
---|
289 | int rc = syscall(__NR_io_getevents, AioContext, cReqsMin, cReqs, paIoResults, pTimeout);
|
---|
290 | if (RT_UNLIKELY(rc == -1))
|
---|
291 | return RTErrConvertFromErrno(errno);
|
---|
292 |
|
---|
293 | return rc;
|
---|
294 | }
|
---|
295 |
|
---|
296 | RTR3DECL(int) RTFileAioGetLimits(PRTFILEAIOLIMITS pAioLimits)
|
---|
297 | {
|
---|
298 | int rc = VINF_SUCCESS;
|
---|
299 | AssertPtrReturn(pAioLimits, VERR_INVALID_POINTER);
|
---|
300 |
|
---|
301 | /*
|
---|
302 | * Check if the API is implemented by creating a
|
---|
303 | * completion port.
|
---|
304 | */
|
---|
305 | LNXKAIOCONTEXT AioContext = 0;
|
---|
306 | rc = rtFileAsyncIoLinuxCreate(1, &AioContext);
|
---|
307 | if (RT_FAILURE(rc))
|
---|
308 | return rc;
|
---|
309 |
|
---|
310 | rc = rtFileAsyncIoLinuxDestroy(AioContext);
|
---|
311 | if (RT_FAILURE(rc))
|
---|
312 | return rc;
|
---|
313 |
|
---|
314 | /* Supported - fill in the limits. The alignment is the only restriction. */
|
---|
315 | pAioLimits->cReqsOutstandingMax = RTFILEAIO_UNLIMITED_REQS;
|
---|
316 | pAioLimits->cbBufferAlignment = 512;
|
---|
317 |
|
---|
318 | return VINF_SUCCESS;
|
---|
319 | }
|
---|
320 |
|
---|
321 |
|
---|
322 | RTR3DECL(int) RTFileAioReqCreate(PRTFILEAIOREQ phReq)
|
---|
323 | {
|
---|
324 | AssertPtrReturn(phReq, VERR_INVALID_POINTER);
|
---|
325 |
|
---|
326 | /*
|
---|
327 | * Allocate a new request and initialize it.
|
---|
328 | */
|
---|
329 | PRTFILEAIOREQINTERNAL pReqInt = (PRTFILEAIOREQINTERNAL)RTMemAllocZ(sizeof(*pReqInt));
|
---|
330 | if (RT_UNLIKELY(!pReqInt))
|
---|
331 | return VERR_NO_MEMORY;
|
---|
332 |
|
---|
333 | pReqInt->pCtxInt = NULL;
|
---|
334 | pReqInt->u32Magic = RTFILEAIOREQ_MAGIC;
|
---|
335 | RTFILEAIOREQ_SET_STATE(pReqInt, COMPLETED);
|
---|
336 |
|
---|
337 | *phReq = (RTFILEAIOREQ)pReqInt;
|
---|
338 | return VINF_SUCCESS;
|
---|
339 | }
|
---|
340 |
|
---|
341 |
|
---|
342 | RTDECL(int) RTFileAioReqDestroy(RTFILEAIOREQ hReq)
|
---|
343 | {
|
---|
344 | /*
|
---|
345 | * Validate the handle and ignore nil.
|
---|
346 | */
|
---|
347 | if (hReq == NIL_RTFILEAIOREQ)
|
---|
348 | return VINF_SUCCESS;
|
---|
349 | PRTFILEAIOREQINTERNAL pReqInt = hReq;
|
---|
350 | RTFILEAIOREQ_VALID_RETURN(pReqInt);
|
---|
351 | RTFILEAIOREQ_NOT_STATE_RETURN_RC(pReqInt, SUBMITTED, VERR_FILE_AIO_IN_PROGRESS);
|
---|
352 |
|
---|
353 | /*
|
---|
354 | * Trash the magic and free it.
|
---|
355 | */
|
---|
356 | ASMAtomicUoWriteU32(&pReqInt->u32Magic, ~RTFILEAIOREQ_MAGIC);
|
---|
357 | RTMemFree(pReqInt);
|
---|
358 | return VINF_SUCCESS;
|
---|
359 | }
|
---|
360 |
|
---|
361 |
|
---|
362 | /**
|
---|
363 | * Worker setting up the request.
|
---|
364 | */
|
---|
365 | DECLINLINE(int) rtFileAioReqPrepareTransfer(RTFILEAIOREQ hReq, RTFILE hFile,
|
---|
366 | uint16_t uTransferDirection,
|
---|
367 | RTFOFF off, void *pvBuf, size_t cbTransfer,
|
---|
368 | void *pvUser)
|
---|
369 | {
|
---|
370 | /*
|
---|
371 | * Validate the input.
|
---|
372 | */
|
---|
373 | PRTFILEAIOREQINTERNAL pReqInt = hReq;
|
---|
374 | RTFILEAIOREQ_VALID_RETURN(pReqInt);
|
---|
375 | RTFILEAIOREQ_NOT_STATE_RETURN_RC(pReqInt, SUBMITTED, VERR_FILE_AIO_IN_PROGRESS);
|
---|
376 | Assert(hFile != NIL_RTFILE);
|
---|
377 |
|
---|
378 | if (uTransferDirection != LNXKAIO_IOCB_CMD_FSYNC)
|
---|
379 | {
|
---|
380 | AssertPtr(pvBuf);
|
---|
381 | Assert(off >= 0);
|
---|
382 | Assert(cbTransfer > 0);
|
---|
383 | }
|
---|
384 |
|
---|
385 | /*
|
---|
386 | * Setup the control block and clear the finished flag.
|
---|
387 | */
|
---|
388 | pReqInt->AioCB.u16IoOpCode = uTransferDirection;
|
---|
389 | pReqInt->AioCB.uFileDesc = RTFileToNative(hFile);
|
---|
390 | pReqInt->AioCB.off = off;
|
---|
391 | pReqInt->AioCB.cbTransfer = cbTransfer;
|
---|
392 | pReqInt->AioCB.pvBuf = pvBuf;
|
---|
393 | pReqInt->AioCB.pvUser = pvUser;
|
---|
394 |
|
---|
395 | pReqInt->pCtxInt = NULL;
|
---|
396 | RTFILEAIOREQ_SET_STATE(pReqInt, PREPARED);
|
---|
397 |
|
---|
398 | return VINF_SUCCESS;
|
---|
399 | }
|
---|
400 |
|
---|
401 |
|
---|
402 | RTDECL(int) RTFileAioReqPrepareRead(RTFILEAIOREQ hReq, RTFILE hFile, RTFOFF off,
|
---|
403 | void *pvBuf, size_t cbRead, void *pvUser)
|
---|
404 | {
|
---|
405 | return rtFileAioReqPrepareTransfer(hReq, hFile, LNXKAIO_IOCB_CMD_READ,
|
---|
406 | off, pvBuf, cbRead, pvUser);
|
---|
407 | }
|
---|
408 |
|
---|
409 |
|
---|
410 | RTDECL(int) RTFileAioReqPrepareWrite(RTFILEAIOREQ hReq, RTFILE hFile, RTFOFF off,
|
---|
411 | void const *pvBuf, size_t cbWrite, void *pvUser)
|
---|
412 | {
|
---|
413 | return rtFileAioReqPrepareTransfer(hReq, hFile, LNXKAIO_IOCB_CMD_WRITE,
|
---|
414 | off, (void *)pvBuf, cbWrite, pvUser);
|
---|
415 | }
|
---|
416 |
|
---|
417 |
|
---|
418 | RTDECL(int) RTFileAioReqPrepareFlush(RTFILEAIOREQ hReq, RTFILE hFile, void *pvUser)
|
---|
419 | {
|
---|
420 | PRTFILEAIOREQINTERNAL pReqInt = hReq;
|
---|
421 | RTFILEAIOREQ_VALID_RETURN(pReqInt);
|
---|
422 | AssertReturn(hFile != NIL_RTFILE, VERR_INVALID_HANDLE);
|
---|
423 | RTFILEAIOREQ_NOT_STATE_RETURN_RC(pReqInt, SUBMITTED, VERR_FILE_AIO_IN_PROGRESS);
|
---|
424 |
|
---|
425 | return rtFileAioReqPrepareTransfer(pReqInt, hFile, LNXKAIO_IOCB_CMD_FSYNC,
|
---|
426 | 0, NULL, 0, pvUser);
|
---|
427 | }
|
---|
428 |
|
---|
429 |
|
---|
430 | RTDECL(void *) RTFileAioReqGetUser(RTFILEAIOREQ hReq)
|
---|
431 | {
|
---|
432 | PRTFILEAIOREQINTERNAL pReqInt = hReq;
|
---|
433 | RTFILEAIOREQ_VALID_RETURN_RC(pReqInt, NULL);
|
---|
434 |
|
---|
435 | return pReqInt->AioCB.pvUser;
|
---|
436 | }
|
---|
437 |
|
---|
438 |
|
---|
439 | RTDECL(int) RTFileAioReqCancel(RTFILEAIOREQ hReq)
|
---|
440 | {
|
---|
441 | PRTFILEAIOREQINTERNAL pReqInt = hReq;
|
---|
442 | RTFILEAIOREQ_VALID_RETURN(pReqInt);
|
---|
443 | RTFILEAIOREQ_STATE_RETURN_RC(pReqInt, SUBMITTED, VERR_FILE_AIO_NOT_SUBMITTED);
|
---|
444 |
|
---|
445 | LNXKAIOIOEVENT AioEvent;
|
---|
446 | int rc = rtFileAsyncIoLinuxCancel(pReqInt->AioContext, &pReqInt->AioCB, &AioEvent);
|
---|
447 | if (RT_SUCCESS(rc))
|
---|
448 | {
|
---|
449 | /*
|
---|
450 | * Decrement request count because the request will never arrive at the
|
---|
451 | * completion port.
|
---|
452 | */
|
---|
453 | AssertMsg(VALID_PTR(pReqInt->pCtxInt),
|
---|
454 | ("Invalid state. Request was canceled but wasn't submitted\n"));
|
---|
455 |
|
---|
456 | ASMAtomicDecS32(&pReqInt->pCtxInt->cRequests);
|
---|
457 | pReqInt->Rc = VERR_FILE_AIO_CANCELED;
|
---|
458 | RTFILEAIOREQ_SET_STATE(pReqInt, COMPLETED);
|
---|
459 | return VINF_SUCCESS;
|
---|
460 | }
|
---|
461 | if (rc == VERR_TRY_AGAIN)
|
---|
462 | return VERR_FILE_AIO_IN_PROGRESS;
|
---|
463 | return rc;
|
---|
464 | }
|
---|
465 |
|
---|
466 |
|
---|
467 | RTDECL(int) RTFileAioReqGetRC(RTFILEAIOREQ hReq, size_t *pcbTransfered)
|
---|
468 | {
|
---|
469 | PRTFILEAIOREQINTERNAL pReqInt = hReq;
|
---|
470 | RTFILEAIOREQ_VALID_RETURN(pReqInt);
|
---|
471 | AssertPtrNull(pcbTransfered);
|
---|
472 | RTFILEAIOREQ_NOT_STATE_RETURN_RC(pReqInt, SUBMITTED, VERR_FILE_AIO_IN_PROGRESS);
|
---|
473 | RTFILEAIOREQ_NOT_STATE_RETURN_RC(pReqInt, PREPARED, VERR_FILE_AIO_NOT_SUBMITTED);
|
---|
474 |
|
---|
475 | if ( pcbTransfered
|
---|
476 | && RT_SUCCESS(pReqInt->Rc))
|
---|
477 | *pcbTransfered = pReqInt->cbTransfered;
|
---|
478 |
|
---|
479 | return pReqInt->Rc;
|
---|
480 | }
|
---|
481 |
|
---|
482 |
|
---|
483 | RTDECL(int) RTFileAioCtxCreate(PRTFILEAIOCTX phAioCtx, uint32_t cAioReqsMax,
|
---|
484 | uint32_t fFlags)
|
---|
485 | {
|
---|
486 | PRTFILEAIOCTXINTERNAL pCtxInt;
|
---|
487 | AssertPtrReturn(phAioCtx, VERR_INVALID_POINTER);
|
---|
488 | AssertReturn(!(fFlags & ~RTFILEAIOCTX_FLAGS_VALID_MASK), VERR_INVALID_PARAMETER);
|
---|
489 |
|
---|
490 | /* The kernel interface needs a maximum. */
|
---|
491 | if (cAioReqsMax == RTFILEAIO_UNLIMITED_REQS)
|
---|
492 | return VERR_OUT_OF_RANGE;
|
---|
493 |
|
---|
494 | pCtxInt = (PRTFILEAIOCTXINTERNAL)RTMemAllocZ(sizeof(RTFILEAIOCTXINTERNAL));
|
---|
495 | if (RT_UNLIKELY(!pCtxInt))
|
---|
496 | return VERR_NO_MEMORY;
|
---|
497 |
|
---|
498 | /* Init the event handle. */
|
---|
499 | int rc = rtFileAsyncIoLinuxCreate(cAioReqsMax, &pCtxInt->AioContext);
|
---|
500 | if (RT_SUCCESS(rc))
|
---|
501 | {
|
---|
502 | pCtxInt->fWokenUp = false;
|
---|
503 | pCtxInt->fWaiting = false;
|
---|
504 | pCtxInt->hThreadWait = NIL_RTTHREAD;
|
---|
505 | pCtxInt->cRequestsMax = cAioReqsMax;
|
---|
506 | pCtxInt->fFlags = fFlags;
|
---|
507 | pCtxInt->u32Magic = RTFILEAIOCTX_MAGIC;
|
---|
508 | *phAioCtx = (RTFILEAIOCTX)pCtxInt;
|
---|
509 | }
|
---|
510 | else
|
---|
511 | RTMemFree(pCtxInt);
|
---|
512 |
|
---|
513 | return rc;
|
---|
514 | }
|
---|
515 |
|
---|
516 |
|
---|
517 | RTDECL(int) RTFileAioCtxDestroy(RTFILEAIOCTX hAioCtx)
|
---|
518 | {
|
---|
519 | /* Validate the handle and ignore nil. */
|
---|
520 | if (hAioCtx == NIL_RTFILEAIOCTX)
|
---|
521 | return VINF_SUCCESS;
|
---|
522 | PRTFILEAIOCTXINTERNAL pCtxInt = hAioCtx;
|
---|
523 | RTFILEAIOCTX_VALID_RETURN(pCtxInt);
|
---|
524 |
|
---|
525 | /* Cannot destroy a busy context. */
|
---|
526 | if (RT_UNLIKELY(pCtxInt->cRequests))
|
---|
527 | return VERR_FILE_AIO_BUSY;
|
---|
528 |
|
---|
529 | /* The native bit first, then mark it as dead and free it. */
|
---|
530 | int rc = rtFileAsyncIoLinuxDestroy(pCtxInt->AioContext);
|
---|
531 | if (RT_FAILURE(rc))
|
---|
532 | return rc;
|
---|
533 | ASMAtomicUoWriteU32(&pCtxInt->u32Magic, RTFILEAIOCTX_MAGIC_DEAD);
|
---|
534 | RTMemFree(pCtxInt);
|
---|
535 |
|
---|
536 | return VINF_SUCCESS;
|
---|
537 | }
|
---|
538 |
|
---|
539 |
|
---|
540 | RTDECL(uint32_t) RTFileAioCtxGetMaxReqCount(RTFILEAIOCTX hAioCtx)
|
---|
541 | {
|
---|
542 | /* Nil means global here. */
|
---|
543 | if (hAioCtx == NIL_RTFILEAIOCTX)
|
---|
544 | return RTFILEAIO_UNLIMITED_REQS; /** @todo r=bird: I'm a bit puzzled by this return value since it
|
---|
545 | * is completely useless in RTFileAioCtxCreate. */
|
---|
546 |
|
---|
547 | /* Return 0 if the handle is invalid, it's better than garbage I think... */
|
---|
548 | PRTFILEAIOCTXINTERNAL pCtxInt = hAioCtx;
|
---|
549 | RTFILEAIOCTX_VALID_RETURN_RC(pCtxInt, 0);
|
---|
550 |
|
---|
551 | return pCtxInt->cRequestsMax;
|
---|
552 | }
|
---|
553 |
|
---|
554 | RTDECL(int) RTFileAioCtxAssociateWithFile(RTFILEAIOCTX hAioCtx, RTFILE hFile)
|
---|
555 | {
|
---|
556 | /* Nothing to do. */
|
---|
557 | NOREF(hAioCtx); NOREF(hFile);
|
---|
558 | return VINF_SUCCESS;
|
---|
559 | }
|
---|
560 |
|
---|
561 | RTDECL(int) RTFileAioCtxSubmit(RTFILEAIOCTX hAioCtx, PRTFILEAIOREQ pahReqs, size_t cReqs)
|
---|
562 | {
|
---|
563 | int rc = VINF_SUCCESS;
|
---|
564 |
|
---|
565 | /*
|
---|
566 | * Parameter validation.
|
---|
567 | */
|
---|
568 | PRTFILEAIOCTXINTERNAL pCtxInt = hAioCtx;
|
---|
569 | RTFILEAIOCTX_VALID_RETURN(pCtxInt);
|
---|
570 | AssertReturn(cReqs > 0, VERR_INVALID_PARAMETER);
|
---|
571 | AssertPtrReturn(pahReqs, VERR_INVALID_POINTER);
|
---|
572 | uint32_t i = cReqs;
|
---|
573 | PRTFILEAIOREQINTERNAL pReqInt = NULL;
|
---|
574 |
|
---|
575 | /*
|
---|
576 | * Validate requests and associate with the context.
|
---|
577 | */
|
---|
578 | while (i-- > 0)
|
---|
579 | {
|
---|
580 | pReqInt = pahReqs[i];
|
---|
581 | if (RTFILEAIOREQ_IS_NOT_VALID(pReqInt))
|
---|
582 | {
|
---|
583 | /* Undo everything and stop submitting. */
|
---|
584 | size_t iUndo = cReqs;
|
---|
585 | while (iUndo-- > i)
|
---|
586 | {
|
---|
587 | pReqInt = pahReqs[iUndo];
|
---|
588 | RTFILEAIOREQ_SET_STATE(pReqInt, PREPARED);
|
---|
589 | pReqInt->pCtxInt = NULL;
|
---|
590 | }
|
---|
591 | return VERR_INVALID_HANDLE;
|
---|
592 | }
|
---|
593 |
|
---|
594 | pReqInt->AioContext = pCtxInt->AioContext;
|
---|
595 | pReqInt->pCtxInt = pCtxInt;
|
---|
596 | RTFILEAIOREQ_SET_STATE(pReqInt, SUBMITTED);
|
---|
597 | }
|
---|
598 |
|
---|
599 | do
|
---|
600 | {
|
---|
601 | /*
|
---|
602 | * We cast pahReqs to the Linux iocb structure to avoid copying the requests
|
---|
603 | * into a temporary array. This is possible because the iocb structure is
|
---|
604 | * the first element in the request structure (see PRTFILEAIOCTXINTERNAL).
|
---|
605 | */
|
---|
606 | int cReqsSubmitted = 0;
|
---|
607 | rc = rtFileAsyncIoLinuxSubmit(pCtxInt->AioContext, cReqs,
|
---|
608 | (PLNXKAIOIOCB *)pahReqs,
|
---|
609 | &cReqsSubmitted);
|
---|
610 | if (RT_FAILURE(rc))
|
---|
611 | {
|
---|
612 | /*
|
---|
613 | * We encountered an error.
|
---|
614 | * This means that the first IoCB
|
---|
615 | * is not correctly initialized
|
---|
616 | * (invalid buffer alignment or bad file descriptor).
|
---|
617 | * Revert every request into the prepared state except
|
---|
618 | * the first one which will switch to completed.
|
---|
619 | * Another reason could be insufficient resources.
|
---|
620 | */
|
---|
621 | i = cReqs;
|
---|
622 | while (i-- > 0)
|
---|
623 | {
|
---|
624 | /* Already validated. */
|
---|
625 | pReqInt = pahReqs[i];
|
---|
626 | pReqInt->pCtxInt = NULL;
|
---|
627 | pReqInt->AioContext = 0;
|
---|
628 | RTFILEAIOREQ_SET_STATE(pReqInt, PREPARED);
|
---|
629 | }
|
---|
630 |
|
---|
631 | if (rc == VERR_TRY_AGAIN)
|
---|
632 | return VERR_FILE_AIO_INSUFFICIENT_RESSOURCES;
|
---|
633 | else
|
---|
634 | {
|
---|
635 | /* The first request failed. */
|
---|
636 | pReqInt = pahReqs[0];
|
---|
637 | RTFILEAIOREQ_SET_STATE(pReqInt, COMPLETED);
|
---|
638 | pReqInt->Rc = rc;
|
---|
639 | pReqInt->cbTransfered = 0;
|
---|
640 | return rc;
|
---|
641 | }
|
---|
642 | }
|
---|
643 |
|
---|
644 | /* Advance. */
|
---|
645 | cReqs -= cReqsSubmitted;
|
---|
646 | pahReqs += cReqsSubmitted;
|
---|
647 | ASMAtomicAddS32(&pCtxInt->cRequests, cReqsSubmitted);
|
---|
648 |
|
---|
649 | } while (cReqs);
|
---|
650 |
|
---|
651 | return rc;
|
---|
652 | }
|
---|
653 |
|
---|
654 |
|
---|
655 | RTDECL(int) RTFileAioCtxWait(RTFILEAIOCTX hAioCtx, size_t cMinReqs, RTMSINTERVAL cMillies,
|
---|
656 | PRTFILEAIOREQ pahReqs, size_t cReqs, uint32_t *pcReqs)
|
---|
657 | {
|
---|
658 | /*
|
---|
659 | * Validate the parameters, making sure to always set pcReqs.
|
---|
660 | */
|
---|
661 | AssertPtrReturn(pcReqs, VERR_INVALID_POINTER);
|
---|
662 | *pcReqs = 0; /* always set */
|
---|
663 | PRTFILEAIOCTXINTERNAL pCtxInt = hAioCtx;
|
---|
664 | RTFILEAIOCTX_VALID_RETURN(pCtxInt);
|
---|
665 | AssertPtrReturn(pahReqs, VERR_INVALID_POINTER);
|
---|
666 | AssertReturn(cReqs != 0, VERR_INVALID_PARAMETER);
|
---|
667 | AssertReturn(cReqs >= cMinReqs, VERR_OUT_OF_RANGE);
|
---|
668 |
|
---|
669 | /*
|
---|
670 | * Can't wait if there are not requests around.
|
---|
671 | */
|
---|
672 | if ( RT_UNLIKELY(ASMAtomicUoReadS32(&pCtxInt->cRequests) == 0)
|
---|
673 | && !(pCtxInt->fFlags & RTFILEAIOCTX_FLAGS_WAIT_WITHOUT_PENDING_REQUESTS))
|
---|
674 | return VERR_FILE_AIO_NO_REQUEST;
|
---|
675 |
|
---|
676 | /*
|
---|
677 | * Convert the timeout if specified.
|
---|
678 | */
|
---|
679 | struct timespec *pTimeout = NULL;
|
---|
680 | struct timespec Timeout = {0,0};
|
---|
681 | uint64_t StartNanoTS = 0;
|
---|
682 | if (cMillies != RT_INDEFINITE_WAIT)
|
---|
683 | {
|
---|
684 | Timeout.tv_sec = cMillies / 1000;
|
---|
685 | Timeout.tv_nsec = cMillies % 1000 * 1000000;
|
---|
686 | pTimeout = &Timeout;
|
---|
687 | StartNanoTS = RTTimeNanoTS();
|
---|
688 | }
|
---|
689 |
|
---|
690 | /* Wait for at least one. */
|
---|
691 | if (!cMinReqs)
|
---|
692 | cMinReqs = 1;
|
---|
693 |
|
---|
694 | /* For the wakeup call. */
|
---|
695 | Assert(pCtxInt->hThreadWait == NIL_RTTHREAD);
|
---|
696 | ASMAtomicWriteHandle(&pCtxInt->hThreadWait, RTThreadSelf());
|
---|
697 |
|
---|
698 | /*
|
---|
699 | * Loop until we're woken up, hit an error (incl timeout), or
|
---|
700 | * have collected the desired number of requests.
|
---|
701 | */
|
---|
702 | int rc = VINF_SUCCESS;
|
---|
703 | int cRequestsCompleted = 0;
|
---|
704 | while (!pCtxInt->fWokenUp)
|
---|
705 | {
|
---|
706 | LNXKAIOIOEVENT aPortEvents[AIO_MAXIMUM_REQUESTS_PER_CONTEXT];
|
---|
707 | int cRequestsToWait = RT_MIN(cReqs, AIO_MAXIMUM_REQUESTS_PER_CONTEXT);
|
---|
708 | ASMAtomicXchgBool(&pCtxInt->fWaiting, true);
|
---|
709 | rc = rtFileAsyncIoLinuxGetEvents(pCtxInt->AioContext, cMinReqs, cRequestsToWait, &aPortEvents[0], pTimeout);
|
---|
710 | ASMAtomicXchgBool(&pCtxInt->fWaiting, false);
|
---|
711 | if (RT_FAILURE(rc))
|
---|
712 | break;
|
---|
713 | uint32_t const cDone = rc;
|
---|
714 | rc = VINF_SUCCESS;
|
---|
715 |
|
---|
716 | /*
|
---|
717 | * Process received events / requests.
|
---|
718 | */
|
---|
719 | for (uint32_t i = 0; i < cDone; i++)
|
---|
720 | {
|
---|
721 | /*
|
---|
722 | * The iocb is the first element in our request structure.
|
---|
723 | * So we can safely cast it directly to the handle (see above)
|
---|
724 | */
|
---|
725 | PRTFILEAIOREQINTERNAL pReqInt = (PRTFILEAIOREQINTERNAL)aPortEvents[i].pIoCB;
|
---|
726 | AssertPtr(pReqInt);
|
---|
727 | Assert(pReqInt->u32Magic == RTFILEAIOREQ_MAGIC);
|
---|
728 |
|
---|
729 | /** @todo aeichner: The rc field contains the result code
|
---|
730 | * like you can find in errno for the normal read/write ops.
|
---|
731 | * But there is a second field called rc2. I don't know the
|
---|
732 | * purpose for it yet.
|
---|
733 | */
|
---|
734 | if (RT_UNLIKELY(aPortEvents[i].rc < 0))
|
---|
735 | pReqInt->Rc = RTErrConvertFromErrno(-aPortEvents[i].rc); /* Convert to positive value. */
|
---|
736 | else
|
---|
737 | {
|
---|
738 | pReqInt->Rc = VINF_SUCCESS;
|
---|
739 | pReqInt->cbTransfered = aPortEvents[i].rc;
|
---|
740 | }
|
---|
741 |
|
---|
742 | /* Mark the request as finished. */
|
---|
743 | RTFILEAIOREQ_SET_STATE(pReqInt, COMPLETED);
|
---|
744 |
|
---|
745 | pahReqs[cRequestsCompleted++] = (RTFILEAIOREQ)pReqInt;
|
---|
746 | }
|
---|
747 |
|
---|
748 | /*
|
---|
749 | * Done Yet? If not advance and try again.
|
---|
750 | */
|
---|
751 | if (cDone >= cMinReqs)
|
---|
752 | break;
|
---|
753 | cMinReqs -= cDone;
|
---|
754 | cReqs -= cDone;
|
---|
755 |
|
---|
756 | if (cMillies != RT_INDEFINITE_WAIT)
|
---|
757 | {
|
---|
758 | /* The API doesn't return ETIMEDOUT, so we have to fix that ourselves. */
|
---|
759 | uint64_t NanoTS = RTTimeNanoTS();
|
---|
760 | uint64_t cMilliesElapsed = (NanoTS - StartNanoTS) / 1000000;
|
---|
761 | if (cMilliesElapsed >= cMillies)
|
---|
762 | {
|
---|
763 | rc = VERR_TIMEOUT;
|
---|
764 | break;
|
---|
765 | }
|
---|
766 |
|
---|
767 | /* The syscall supposedly updates it, but we're paranoid. :-) */
|
---|
768 | Timeout.tv_sec = (cMillies - (RTMSINTERVAL)cMilliesElapsed) / 1000;
|
---|
769 | Timeout.tv_nsec = (cMillies - (RTMSINTERVAL)cMilliesElapsed) % 1000 * 1000000;
|
---|
770 | }
|
---|
771 | }
|
---|
772 |
|
---|
773 | /*
|
---|
774 | * Update the context state and set the return value.
|
---|
775 | */
|
---|
776 | *pcReqs = cRequestsCompleted;
|
---|
777 | ASMAtomicSubS32(&pCtxInt->cRequests, cRequestsCompleted);
|
---|
778 | Assert(pCtxInt->hThreadWait == RTThreadSelf());
|
---|
779 | ASMAtomicWriteHandle(&pCtxInt->hThreadWait, NIL_RTTHREAD);
|
---|
780 |
|
---|
781 | /*
|
---|
782 | * Clear the wakeup flag and set rc.
|
---|
783 | */
|
---|
784 | if ( pCtxInt->fWokenUp
|
---|
785 | && RT_SUCCESS(rc))
|
---|
786 | {
|
---|
787 | ASMAtomicXchgBool(&pCtxInt->fWokenUp, false);
|
---|
788 | rc = VERR_INTERRUPTED;
|
---|
789 | }
|
---|
790 |
|
---|
791 | return rc;
|
---|
792 | }
|
---|
793 |
|
---|
794 |
|
---|
795 | RTDECL(int) RTFileAioCtxWakeup(RTFILEAIOCTX hAioCtx)
|
---|
796 | {
|
---|
797 | PRTFILEAIOCTXINTERNAL pCtxInt = hAioCtx;
|
---|
798 | RTFILEAIOCTX_VALID_RETURN(pCtxInt);
|
---|
799 |
|
---|
800 | /** @todo r=bird: Define the protocol for how to resume work after calling
|
---|
801 | * this function. */
|
---|
802 |
|
---|
803 | bool fWokenUp = ASMAtomicXchgBool(&pCtxInt->fWokenUp, true);
|
---|
804 |
|
---|
805 | /*
|
---|
806 | * Read the thread handle before the status flag.
|
---|
807 | * If we read the handle after the flag we might
|
---|
808 | * end up with an invalid handle because the thread
|
---|
809 | * waiting in RTFileAioCtxWakeup() might get scheduled
|
---|
810 | * before we read the flag and returns.
|
---|
811 | * We can ensure that the handle is valid if fWaiting is true
|
---|
812 | * when reading the handle before the status flag.
|
---|
813 | */
|
---|
814 | RTTHREAD hThread;
|
---|
815 | ASMAtomicReadHandle(&pCtxInt->hThreadWait, &hThread);
|
---|
816 | bool fWaiting = ASMAtomicReadBool(&pCtxInt->fWaiting);
|
---|
817 | if ( !fWokenUp
|
---|
818 | && fWaiting)
|
---|
819 | {
|
---|
820 | /*
|
---|
821 | * If a thread waits the handle must be valid.
|
---|
822 | * It is possible that the thread returns from
|
---|
823 | * rtFileAsyncIoLinuxGetEvents() before the signal
|
---|
824 | * is send.
|
---|
825 | * This is no problem because we already set fWokenUp
|
---|
826 | * to true which will let the thread return VERR_INTERRUPTED
|
---|
827 | * and the next call to RTFileAioCtxWait() will not
|
---|
828 | * return VERR_INTERRUPTED because signals are not saved
|
---|
829 | * and will simply vanish if the destination thread can't
|
---|
830 | * receive it.
|
---|
831 | */
|
---|
832 | Assert(hThread != NIL_RTTHREAD);
|
---|
833 | RTThreadPoke(hThread);
|
---|
834 | }
|
---|
835 |
|
---|
836 | return VINF_SUCCESS;
|
---|
837 | }
|
---|
838 |
|
---|