VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/solaris/fileaio-solaris.cpp@ 103914

Last change on this file since 103914 was 98103, checked in by vboxsync, 23 months ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 18.6 KB
Line 
1/* $Id: fileaio-solaris.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
2/** @file
3 * IPRT - File async I/O, native implementation for the Solaris host platform.
4 */
5
6/*
7 * Copyright (C) 2006-2023 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * The contents of this file may alternatively be used under the terms
26 * of the Common Development and Distribution License Version 1.0
27 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
28 * in the VirtualBox distribution, in which case the provisions of the
29 * CDDL are applicable instead of those of the GPL.
30 *
31 * You may elect to license modified versions of this file under the
32 * terms and conditions of either the GPL or the CDDL or both.
33 *
34 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
35 */
36
37
38/*********************************************************************************************************************************
39* Header Files *
40*********************************************************************************************************************************/
41#define LOG_GROUP RTLOGGROUP_FILE
42#include <iprt/asm.h>
43#include <iprt/file.h>
44#include <iprt/mem.h>
45#include <iprt/assert.h>
46#include <iprt/string.h>
47#include <iprt/err.h>
48#include <iprt/log.h>
49#include "internal/fileaio.h"
50
51#include <port.h>
52#include <aio.h>
53#include <errno.h>
54#include <unistd.h>
55
56
57/*********************************************************************************************************************************
58* Structures and Typedefs *
59*********************************************************************************************************************************/
60/**
61 * Async I/O completion context state.
62 */
63typedef struct RTFILEAIOCTXINTERNAL
64{
65 /** Handle to the port. */
66 int iPort;
67 /** Current number of requests active on this context. */
68 volatile int32_t cRequests;
69 /** Flags given during creation. */
70 uint32_t fFlags;
71 /** Magic value (RTFILEAIOCTX_MAGIC). */
72 uint32_t u32Magic;
73} RTFILEAIOCTXINTERNAL;
74/** Pointer to an internal context structure. */
75typedef RTFILEAIOCTXINTERNAL *PRTFILEAIOCTXINTERNAL;
76
77/**
78 * Async I/O request state.
79 */
80typedef struct RTFILEAIOREQINTERNAL
81{
82 /** The aio control block. Must be the FIRST
83 * element. */
84 struct aiocb AioCB;
85 /** Current state the request is in. */
86 RTFILEAIOREQSTATE enmState;
87 /** Flag whether this is a flush request. */
88 bool fFlush;
89 /** Port notifier object to associate a request to a port. */
90 port_notify_t PortNotifier;
91 /** Opaque user data. */
92 void *pvUser;
93 /** Completion context we are assigned to. */
94 PRTFILEAIOCTXINTERNAL pCtxInt;
95 /** Magic value (RTFILEAIOREQ_MAGIC). */
96 uint32_t u32Magic;
97} RTFILEAIOREQINTERNAL;
98/** Pointer to an internal request structure. */
99typedef RTFILEAIOREQINTERNAL *PRTFILEAIOREQINTERNAL;
100
101
102/*********************************************************************************************************************************
103* Defined Constants And Macros *
104*********************************************************************************************************************************/
105/** The max number of events to get in one call. */
106#define AIO_MAXIMUM_REQUESTS_PER_CONTEXT 64
107/** Id for the wakeup event. */
108#define AIO_CONTEXT_WAKEUP_EVENT 1
109
110RTR3DECL(int) RTFileAioGetLimits(PRTFILEAIOLIMITS pAioLimits)
111{
112 int rcBSD = 0;
113 AssertPtrReturn(pAioLimits, VERR_INVALID_POINTER);
114
115 /* No limits known. */
116 pAioLimits->cReqsOutstandingMax = RTFILEAIO_UNLIMITED_REQS;
117 pAioLimits->cbBufferAlignment = 0;
118
119 return VINF_SUCCESS;
120}
121
122RTR3DECL(int) RTFileAioReqCreate(PRTFILEAIOREQ phReq)
123{
124 AssertPtrReturn(phReq, VERR_INVALID_POINTER);
125
126 PRTFILEAIOREQINTERNAL pReqInt = (PRTFILEAIOREQINTERNAL)RTMemAllocZ(sizeof(RTFILEAIOREQINTERNAL));
127 if (RT_UNLIKELY(!pReqInt))
128 return VERR_NO_MEMORY;
129
130 /* Ininitialize static parts. */
131 pReqInt->AioCB.aio_sigevent.sigev_notify = SIGEV_PORT;
132 pReqInt->AioCB.aio_sigevent.sigev_value.sival_ptr = &pReqInt->PortNotifier;
133 pReqInt->PortNotifier.portnfy_user = pReqInt;
134 pReqInt->pCtxInt = NULL;
135 pReqInt->u32Magic = RTFILEAIOREQ_MAGIC;
136 RTFILEAIOREQ_SET_STATE(pReqInt, COMPLETED);
137
138 *phReq = (RTFILEAIOREQ)pReqInt;
139
140 return VINF_SUCCESS;
141}
142
143RTDECL(int) RTFileAioReqDestroy(RTFILEAIOREQ hReq)
144{
145 /*
146 * Validate the handle and ignore nil.
147 */
148 if (hReq == NIL_RTFILEAIOREQ)
149 return VINF_SUCCESS;
150 PRTFILEAIOREQINTERNAL pReqInt = hReq;
151 RTFILEAIOREQ_VALID_RETURN(pReqInt);
152 RTFILEAIOREQ_NOT_STATE_RETURN_RC(pReqInt, SUBMITTED, VERR_FILE_AIO_IN_PROGRESS);
153
154 /*
155 * Trash the magic and free it.
156 */
157 ASMAtomicUoWriteU32(&pReqInt->u32Magic, ~RTFILEAIOREQ_MAGIC);
158 RTMemFree(pReqInt);
159 return VINF_SUCCESS;
160}
161
162/**
163 * Worker setting up the request.
164 */
165DECLINLINE(int) rtFileAioReqPrepareTransfer(RTFILEAIOREQ hReq, RTFILE hFile,
166 unsigned uTransferDirection,
167 RTFOFF off, void *pvBuf, size_t cbTransfer,
168 void *pvUser)
169{
170 /*
171 * Validate the input.
172 */
173 PRTFILEAIOREQINTERNAL pReqInt = hReq;
174 RTFILEAIOREQ_VALID_RETURN(pReqInt);
175 RTFILEAIOREQ_NOT_STATE_RETURN_RC(pReqInt, SUBMITTED, VERR_FILE_AIO_IN_PROGRESS);
176 Assert(hFile != NIL_RTFILE);
177 AssertPtr(pvBuf);
178 Assert(off >= 0);
179 Assert(cbTransfer > 0);
180
181 pReqInt->AioCB.aio_lio_opcode = uTransferDirection;
182 pReqInt->AioCB.aio_fildes = RTFileToNative(hFile);
183 pReqInt->AioCB.aio_offset = off;
184 pReqInt->AioCB.aio_nbytes = cbTransfer;
185 pReqInt->AioCB.aio_buf = pvBuf;
186 pReqInt->fFlush = false;
187 pReqInt->pvUser = pvUser;
188 pReqInt->pCtxInt = NULL;
189 RTFILEAIOREQ_SET_STATE(pReqInt, PREPARED);
190
191 return VINF_SUCCESS;
192}
193
194RTDECL(int) RTFileAioReqPrepareRead(RTFILEAIOREQ hReq, RTFILE hFile, RTFOFF off,
195 void *pvBuf, size_t cbRead, void *pvUser)
196{
197 return rtFileAioReqPrepareTransfer(hReq, hFile, LIO_READ,
198 off, pvBuf, cbRead, pvUser);
199}
200
201RTDECL(int) RTFileAioReqPrepareWrite(RTFILEAIOREQ hReq, RTFILE hFile, RTFOFF off,
202 void const *pvBuf, size_t cbWrite, void *pvUser)
203{
204 return rtFileAioReqPrepareTransfer(hReq, hFile, LIO_WRITE,
205 off, (void *)pvBuf, cbWrite, pvUser);
206}
207
208RTDECL(int) RTFileAioReqPrepareFlush(RTFILEAIOREQ hReq, RTFILE hFile, void *pvUser)
209{
210 PRTFILEAIOREQINTERNAL pReqInt = (PRTFILEAIOREQINTERNAL)hReq;
211
212 RTFILEAIOREQ_VALID_RETURN(pReqInt);
213 RTFILEAIOREQ_NOT_STATE_RETURN_RC(pReqInt, SUBMITTED, VERR_FILE_AIO_IN_PROGRESS);
214 Assert(hFile != NIL_RTFILE);
215
216 pReqInt->fFlush = true;
217 pReqInt->AioCB.aio_fildes = RTFileToNative(hFile);
218 pReqInt->AioCB.aio_offset = 0;
219 pReqInt->AioCB.aio_nbytes = 0;
220 pReqInt->AioCB.aio_buf = NULL;
221 pReqInt->pvUser = pvUser;
222 RTFILEAIOREQ_SET_STATE(pReqInt, PREPARED);
223
224 return VINF_SUCCESS;
225}
226
227RTDECL(void *) RTFileAioReqGetUser(RTFILEAIOREQ hReq)
228{
229 PRTFILEAIOREQINTERNAL pReqInt = hReq;
230 RTFILEAIOREQ_VALID_RETURN_RC(pReqInt, NULL);
231
232 return pReqInt->pvUser;
233}
234
235RTDECL(int) RTFileAioReqCancel(RTFILEAIOREQ hReq)
236{
237 PRTFILEAIOREQINTERNAL pReqInt = hReq;
238 RTFILEAIOREQ_VALID_RETURN(pReqInt);
239 RTFILEAIOREQ_STATE_RETURN_RC(pReqInt, SUBMITTED, VERR_FILE_AIO_NOT_SUBMITTED);
240
241 int rcSolaris = aio_cancel(pReqInt->AioCB.aio_fildes, &pReqInt->AioCB);
242
243 if (rcSolaris == AIO_CANCELED)
244 {
245 /*
246 * Decrement request count because the request will never arrive at the
247 * completion port.
248 */
249 AssertMsg(RT_VALID_PTR(pReqInt->pCtxInt), ("Invalid state. Request was canceled but wasn't submitted\n"));
250
251 ASMAtomicDecS32(&pReqInt->pCtxInt->cRequests);
252 RTFILEAIOREQ_SET_STATE(pReqInt, COMPLETED);
253 return VINF_SUCCESS;
254 }
255 else if (rcSolaris == AIO_ALLDONE)
256 return VERR_FILE_AIO_COMPLETED;
257 else if (rcSolaris == AIO_NOTCANCELED)
258 return VERR_FILE_AIO_IN_PROGRESS;
259 else
260 return RTErrConvertFromErrno(errno);
261}
262
263RTDECL(int) RTFileAioReqGetRC(RTFILEAIOREQ hReq, size_t *pcbTransfered)
264{
265 PRTFILEAIOREQINTERNAL pReqInt = hReq;
266 RTFILEAIOREQ_VALID_RETURN(pReqInt);
267 RTFILEAIOREQ_NOT_STATE_RETURN_RC(pReqInt, SUBMITTED, VERR_FILE_AIO_IN_PROGRESS);
268 RTFILEAIOREQ_NOT_STATE_RETURN_RC(pReqInt, PREPARED, VERR_FILE_AIO_NOT_SUBMITTED);
269 AssertPtrNull(pcbTransfered);
270
271 int rcSol = aio_error(&pReqInt->AioCB);
272 Assert(rcSol != EINPROGRESS); /* Handled by our own state handling. */
273
274 if (rcSol == 0)
275 {
276 if (pcbTransfered)
277 *pcbTransfered = aio_return(&pReqInt->AioCB);
278 return VINF_SUCCESS;
279 }
280
281 /* An error occurred. */
282 return RTErrConvertFromErrno(rcSol);
283}
284
285RTDECL(int) RTFileAioCtxCreate(PRTFILEAIOCTX phAioCtx, uint32_t cAioReqsMax,
286 uint32_t fFlags)
287{
288 int rc = VINF_SUCCESS;
289 PRTFILEAIOCTXINTERNAL pCtxInt;
290 AssertPtrReturn(phAioCtx, VERR_INVALID_POINTER);
291 AssertReturn(!(fFlags & ~RTFILEAIOCTX_FLAGS_VALID_MASK), VERR_INVALID_PARAMETER);
292
293 pCtxInt = (PRTFILEAIOCTXINTERNAL)RTMemAllocZ(sizeof(RTFILEAIOCTXINTERNAL));
294 if (RT_UNLIKELY(!pCtxInt))
295 return VERR_NO_MEMORY;
296
297 /* Init the event handle. */
298 pCtxInt->iPort = port_create();
299 if (RT_LIKELY(pCtxInt->iPort > 0))
300 {
301 pCtxInt->fFlags = fFlags;
302 pCtxInt->u32Magic = RTFILEAIOCTX_MAGIC;
303 *phAioCtx = (RTFILEAIOCTX)pCtxInt;
304 }
305 else
306 {
307 RTMemFree(pCtxInt);
308 rc = RTErrConvertFromErrno(errno);
309 }
310
311 return rc;
312}
313
314RTDECL(int) RTFileAioCtxDestroy(RTFILEAIOCTX hAioCtx)
315{
316 /* Validate the handle and ignore nil. */
317 if (hAioCtx == NIL_RTFILEAIOCTX)
318 return VINF_SUCCESS;
319 PRTFILEAIOCTXINTERNAL pCtxInt = hAioCtx;
320 RTFILEAIOCTX_VALID_RETURN(pCtxInt);
321
322 /* Cannot destroy a busy context. */
323 if (RT_UNLIKELY(pCtxInt->cRequests))
324 return VERR_FILE_AIO_BUSY;
325
326 close(pCtxInt->iPort);
327 ASMAtomicUoWriteU32(&pCtxInt->u32Magic, RTFILEAIOCTX_MAGIC_DEAD);
328 RTMemFree(pCtxInt);
329
330 return VINF_SUCCESS;
331}
332
333RTDECL(uint32_t) RTFileAioCtxGetMaxReqCount(RTFILEAIOCTX hAioCtx)
334{
335 return RTFILEAIO_UNLIMITED_REQS;
336}
337
338RTDECL(int) RTFileAioCtxAssociateWithFile(RTFILEAIOCTX hAioCtx, RTFILE hFile)
339{
340 return VINF_SUCCESS;
341}
342
343RTDECL(int) RTFileAioCtxSubmit(RTFILEAIOCTX hAioCtx, PRTFILEAIOREQ pahReqs, size_t cReqs)
344{
345 /*
346 * Parameter validation.
347 */
348 int rc = VINF_SUCCESS;
349 PRTFILEAIOCTXINTERNAL pCtxInt = hAioCtx;
350 RTFILEAIOCTX_VALID_RETURN(pCtxInt);
351 AssertReturn(cReqs > 0, VERR_INVALID_PARAMETER);
352 AssertPtrReturn(pahReqs, VERR_INVALID_POINTER);
353 size_t i = cReqs;
354
355 do
356 {
357 int rcSol = 0;
358 size_t cReqsSubmit = 0;
359 PRTFILEAIOREQINTERNAL pReqInt;
360
361 while(i-- > 0)
362 {
363 pReqInt = pahReqs[i];
364 if (RTFILEAIOREQ_IS_NOT_VALID(pReqInt))
365 {
366 /* Undo everything and stop submitting. */
367 for (size_t iUndo = 0; iUndo < i; iUndo++)
368 {
369 pReqInt = pahReqs[iUndo];
370 RTFILEAIOREQ_SET_STATE(pReqInt, PREPARED);
371 pReqInt->pCtxInt = NULL;
372 }
373 rc = VERR_INVALID_HANDLE;
374 break;
375 }
376
377 pReqInt->PortNotifier.portnfy_port = pCtxInt->iPort;
378 pReqInt->pCtxInt = pCtxInt;
379 RTFILEAIOREQ_SET_STATE(pReqInt, SUBMITTED);
380
381 if (pReqInt->fFlush)
382 break;
383
384 cReqsSubmit++;
385 }
386
387 if (cReqsSubmit)
388 {
389 rcSol = lio_listio(LIO_NOWAIT, (struct aiocb **)pahReqs, cReqsSubmit, NULL);
390 if (RT_UNLIKELY(rcSol < 0))
391 {
392 if (rcSol == EAGAIN)
393 rc = VERR_FILE_AIO_INSUFFICIENT_RESSOURCES;
394 else
395 rc = RTErrConvertFromErrno(errno);
396
397 /* Check which requests got actually submitted and which not. */
398 for (i = 0; i < cReqs; i++)
399 {
400 pReqInt = pahReqs[i];
401 rcSol = aio_error(&pReqInt->AioCB);
402 if (rcSol == EINVAL)
403 {
404 /* Was not submitted. */
405 RTFILEAIOREQ_SET_STATE(pReqInt, PREPARED);
406 pReqInt->pCtxInt = NULL;
407 }
408 else if (rcSol != EINPROGRESS)
409 {
410 /* The request encountered an error. */
411 RTFILEAIOREQ_SET_STATE(pReqInt, COMPLETED);
412 }
413 }
414 break;
415 }
416
417 ASMAtomicAddS32(&pCtxInt->cRequests, cReqsSubmit);
418 cReqs -= cReqsSubmit;
419 pahReqs += cReqsSubmit;
420 }
421
422 if (cReqs)
423 {
424 pReqInt = pahReqs[0];
425 RTFILEAIOREQ_VALID_RETURN(pReqInt);
426
427 /*
428 * If there are still requests left we have a flush request.
429 * lio_listio does not work with this requests so
430 * we have to use aio_fsync directly.
431 */
432 rcSol = aio_fsync(O_SYNC, &pReqInt->AioCB);
433 if (RT_UNLIKELY(rcSol < 0))
434 {
435 RTFILEAIOREQ_SET_STATE(pReqInt, COMPLETED);
436 rc = RTErrConvertFromErrno(errno);
437 break;
438 }
439
440 ASMAtomicIncS32(&pCtxInt->cRequests);
441 cReqs--;
442 pahReqs++;
443 }
444 } while (cReqs);
445
446 return rc;
447}
448
449RTDECL(int) RTFileAioCtxWait(RTFILEAIOCTX hAioCtx, size_t cMinReqs, RTMSINTERVAL cMillies,
450 PRTFILEAIOREQ pahReqs, size_t cReqs, uint32_t *pcReqs)
451{
452 int rc = VINF_SUCCESS;
453 int cRequestsCompleted = 0;
454
455 /*
456 * Validate the parameters, making sure to always set pcReqs.
457 */
458 AssertPtrReturn(pcReqs, VERR_INVALID_POINTER);
459 *pcReqs = 0; /* always set */
460 PRTFILEAIOCTXINTERNAL pCtxInt = hAioCtx;
461 RTFILEAIOCTX_VALID_RETURN(pCtxInt);
462 AssertPtrReturn(pahReqs, VERR_INVALID_POINTER);
463 AssertReturn(cReqs != 0, VERR_INVALID_PARAMETER);
464 AssertReturn(cReqs >= cMinReqs, VERR_OUT_OF_RANGE);
465
466 if ( RT_UNLIKELY(ASMAtomicReadS32(&pCtxInt->cRequests) == 0)
467 && !(pCtxInt->fFlags & RTFILEAIOCTX_FLAGS_WAIT_WITHOUT_PENDING_REQUESTS))
468 return VERR_FILE_AIO_NO_REQUEST;
469
470 /*
471 * Convert the timeout if specified.
472 */
473 struct timespec *pTimeout = NULL;
474 struct timespec Timeout = {0,0};
475 uint64_t StartNanoTS = 0;
476 if (cMillies != RT_INDEFINITE_WAIT)
477 {
478 Timeout.tv_sec = cMillies / 1000;
479 Timeout.tv_nsec = cMillies % 1000 * 1000000;
480 pTimeout = &Timeout;
481 StartNanoTS = RTTimeNanoTS();
482 }
483
484 /* Wait for at least one. */
485 if (!cMinReqs)
486 cMinReqs = 1;
487
488 while ( cMinReqs
489 && RT_SUCCESS_NP(rc))
490 {
491 port_event_t aPortEvents[AIO_MAXIMUM_REQUESTS_PER_CONTEXT];
492 uint_t cRequests = cMinReqs;
493 int cRequestsToWait = RT_MIN(cReqs, AIO_MAXIMUM_REQUESTS_PER_CONTEXT);
494 int rcSol;
495 uint64_t StartTime;
496
497 rcSol = port_getn(pCtxInt->iPort, &aPortEvents[0], cRequestsToWait, &cRequests, pTimeout);
498
499 if (RT_UNLIKELY(rcSol < 0))
500 rc = RTErrConvertFromErrno(errno);
501
502 /* Process received events. */
503 for (uint_t i = 0; i < cRequests; i++)
504 {
505 if (aPortEvents[i].portev_source == PORT_SOURCE_ALERT)
506 {
507 Assert(aPortEvents[i].portev_events == AIO_CONTEXT_WAKEUP_EVENT);
508 rc = VERR_INTERRUPTED; /* We've got interrupted. */
509 /* Reset the port. */
510 port_alert(pCtxInt->iPort, PORT_ALERT_SET, 0, NULL);
511 }
512 else
513 {
514 PRTFILEAIOREQINTERNAL pReqInt = (PRTFILEAIOREQINTERNAL)aPortEvents[i].portev_user;
515 AssertPtr(pReqInt);
516 Assert(pReqInt->u32Magic == RTFILEAIOREQ_MAGIC);
517
518 /* A request has finished. */
519 pahReqs[cRequestsCompleted++] = pReqInt;
520
521 /* Mark the request as finished. */
522 RTFILEAIOREQ_SET_STATE(pReqInt, COMPLETED);
523 }
524 }
525
526 /*
527 * Done Yet? If not advance and try again.
528 */
529 if (cRequests >= cMinReqs)
530 break;
531 cMinReqs -= cRequests;
532 cReqs -= cRequests;
533
534 if (cMillies != RT_INDEFINITE_WAIT)
535 {
536 uint64_t NanoTS = RTTimeNanoTS();
537 uint64_t cMilliesElapsed = (NanoTS - StartNanoTS) / 1000000;
538
539 /* The syscall supposedly updates it, but we're paranoid. :-) */
540 if (cMilliesElapsed < cMillies)
541 {
542 Timeout.tv_sec = (cMillies - (RTMSINTERVAL)cMilliesElapsed) / 1000;
543 Timeout.tv_nsec = (cMillies - (RTMSINTERVAL)cMilliesElapsed) % 1000 * 1000000;
544 }
545 else
546 {
547 Timeout.tv_sec = 0;
548 Timeout.tv_nsec = 0;
549 }
550 }
551 }
552
553 /*
554 * Update the context state and set the return value.
555 */
556 *pcReqs = cRequestsCompleted;
557 ASMAtomicSubS32(&pCtxInt->cRequests, cRequestsCompleted);
558
559 return rc;
560}
561
562RTDECL(int) RTFileAioCtxWakeup(RTFILEAIOCTX hAioCtx)
563{
564 int rc = VINF_SUCCESS;
565 PRTFILEAIOCTXINTERNAL pCtxInt = hAioCtx;
566 RTFILEAIOCTX_VALID_RETURN(pCtxInt);
567
568 rc = port_alert(pCtxInt->iPort, PORT_ALERT_UPDATE, AIO_CONTEXT_WAKEUP_EVENT, NULL);
569 if (RT_UNLIKELY((rc < 0) && (errno != EBUSY)))
570 return RTErrConvertFromErrno(errno);
571
572 return VINF_SUCCESS;
573}
574
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette