fileaio-solaris.cpp revision e64031e20c39650a7bc902a3e1aba613b9415dee
83ef82c0f827540ae1bbbff41542f02cba3f4e95vboxsync * IPRT - File async I/O, native implementation for the Solaris host platform.
e64031e20c39650a7bc902a3e1aba613b9415deevboxsync * Copyright (C) 2006-2010 Oracle Corporation
83ef82c0f827540ae1bbbff41542f02cba3f4e95vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
83ef82c0f827540ae1bbbff41542f02cba3f4e95vboxsync * available from http://www.virtualbox.org. This file is free software;
83ef82c0f827540ae1bbbff41542f02cba3f4e95vboxsync * you can redistribute it and/or modify it under the terms of the GNU
83ef82c0f827540ae1bbbff41542f02cba3f4e95vboxsync * General Public License (GPL) as published by the Free Software
83ef82c0f827540ae1bbbff41542f02cba3f4e95vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
83ef82c0f827540ae1bbbff41542f02cba3f4e95vboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
83ef82c0f827540ae1bbbff41542f02cba3f4e95vboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
83ef82c0f827540ae1bbbff41542f02cba3f4e95vboxsync * The contents of this file may alternatively be used under the terms
83ef82c0f827540ae1bbbff41542f02cba3f4e95vboxsync * of the Common Development and Distribution License Version 1.0
83ef82c0f827540ae1bbbff41542f02cba3f4e95vboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
83ef82c0f827540ae1bbbff41542f02cba3f4e95vboxsync * VirtualBox OSE distribution, in which case the provisions of the
83ef82c0f827540ae1bbbff41542f02cba3f4e95vboxsync * CDDL are applicable instead of those of the GPL.
83ef82c0f827540ae1bbbff41542f02cba3f4e95vboxsync * You may elect to license modified versions of this file under the
83ef82c0f827540ae1bbbff41542f02cba3f4e95vboxsync * terms and conditions of either the GPL or the CDDL or both.
83ef82c0f827540ae1bbbff41542f02cba3f4e95vboxsync/*******************************************************************************
83ef82c0f827540ae1bbbff41542f02cba3f4e95vboxsync* Header Files *
83ef82c0f827540ae1bbbff41542f02cba3f4e95vboxsync*******************************************************************************/
83ef82c0f827540ae1bbbff41542f02cba3f4e95vboxsync/*******************************************************************************
83ef82c0f827540ae1bbbff41542f02cba3f4e95vboxsync* Structures and Typedefs *
83ef82c0f827540ae1bbbff41542f02cba3f4e95vboxsync*******************************************************************************/
83ef82c0f827540ae1bbbff41542f02cba3f4e95vboxsync * Async I/O completion context state.
83ef82c0f827540ae1bbbff41542f02cba3f4e95vboxsync /** Handle to the port. */
83ef82c0f827540ae1bbbff41542f02cba3f4e95vboxsync /** Current number of requests active on this context. */
83ef82c0f827540ae1bbbff41542f02cba3f4e95vboxsync /** Magic value (RTFILEAIOCTX_MAGIC). */
83ef82c0f827540ae1bbbff41542f02cba3f4e95vboxsync/** Pointer to an internal context structure. */
83ef82c0f827540ae1bbbff41542f02cba3f4e95vboxsynctypedef RTFILEAIOCTXINTERNAL *PRTFILEAIOCTXINTERNAL;
83ef82c0f827540ae1bbbff41542f02cba3f4e95vboxsync * Async I/O request state.
83ef82c0f827540ae1bbbff41542f02cba3f4e95vboxsync /** The aio control block. Must be the FIRST
83ef82c0f827540ae1bbbff41542f02cba3f4e95vboxsync * element. */
83ef82c0f827540ae1bbbff41542f02cba3f4e95vboxsync /** Current state the request is in. */
83ef82c0f827540ae1bbbff41542f02cba3f4e95vboxsync /** Flag whether this is a flush request. */
83ef82c0f827540ae1bbbff41542f02cba3f4e95vboxsync /** Port notifier object to associate a request to a port. */
83ef82c0f827540ae1bbbff41542f02cba3f4e95vboxsync /** Opaque user data. */
83ef82c0f827540ae1bbbff41542f02cba3f4e95vboxsync /** Completion context we are assigned to. */
83ef82c0f827540ae1bbbff41542f02cba3f4e95vboxsync /** Magic value (RTFILEAIOREQ_MAGIC). */
83ef82c0f827540ae1bbbff41542f02cba3f4e95vboxsync/** Pointer to an internal request structure. */
83ef82c0f827540ae1bbbff41542f02cba3f4e95vboxsynctypedef RTFILEAIOREQINTERNAL *PRTFILEAIOREQINTERNAL;
83ef82c0f827540ae1bbbff41542f02cba3f4e95vboxsync/*******************************************************************************
83ef82c0f827540ae1bbbff41542f02cba3f4e95vboxsync* Defined Constants And Macros *
83ef82c0f827540ae1bbbff41542f02cba3f4e95vboxsync*******************************************************************************/
83ef82c0f827540ae1bbbff41542f02cba3f4e95vboxsync/** The max number of events to get in one call. */
83ef82c0f827540ae1bbbff41542f02cba3f4e95vboxsync/** Id for the wakeup event. */
83ef82c0f827540ae1bbbff41542f02cba3f4e95vboxsyncRTR3DECL(int) RTFileAioGetLimits(PRTFILEAIOLIMITS pAioLimits)
83ef82c0f827540ae1bbbff41542f02cba3f4e95vboxsync /* No limits known. */
83ef82c0f827540ae1bbbff41542f02cba3f4e95vboxsync pAioLimits->cReqsOutstandingMax = RTFILEAIO_UNLIMITED_REQS;
83ef82c0f827540ae1bbbff41542f02cba3f4e95vboxsyncRTR3DECL(int) RTFileAioReqCreate(PRTFILEAIOREQ phReq)
83ef82c0f827540ae1bbbff41542f02cba3f4e95vboxsync PRTFILEAIOREQINTERNAL pReqInt = (PRTFILEAIOREQINTERNAL)RTMemAllocZ(sizeof(RTFILEAIOREQINTERNAL));
83ef82c0f827540ae1bbbff41542f02cba3f4e95vboxsync /* Ininitialize static parts. */
83ef82c0f827540ae1bbbff41542f02cba3f4e95vboxsync pReqInt->AioCB.aio_sigevent.sigev_notify = SIGEV_PORT;
83ef82c0f827540ae1bbbff41542f02cba3f4e95vboxsync pReqInt->AioCB.aio_sigevent.sigev_value.sival_ptr = &pReqInt->PortNotifier;
83ef82c0f827540ae1bbbff41542f02cba3f4e95vboxsync * Validate the handle and ignore nil.
83ef82c0f827540ae1bbbff41542f02cba3f4e95vboxsync RTFILEAIOREQ_NOT_STATE_RETURN_RC(pReqInt, SUBMITTED, VERR_FILE_AIO_IN_PROGRESS);
83ef82c0f827540ae1bbbff41542f02cba3f4e95vboxsync * Trash the magic and free it.
83ef82c0f827540ae1bbbff41542f02cba3f4e95vboxsync ASMAtomicUoWriteU32(&pReqInt->u32Magic, ~RTFILEAIOREQ_MAGIC);
83ef82c0f827540ae1bbbff41542f02cba3f4e95vboxsync * Worker setting up the request.
83ef82c0f827540ae1bbbff41542f02cba3f4e95vboxsyncDECLINLINE(int) rtFileAioReqPrepareTransfer(RTFILEAIOREQ hReq, RTFILE hFile,
83ef82c0f827540ae1bbbff41542f02cba3f4e95vboxsync * Validate the input.
83ef82c0f827540ae1bbbff41542f02cba3f4e95vboxsync RTFILEAIOREQ_NOT_STATE_RETURN_RC(pReqInt, SUBMITTED, VERR_FILE_AIO_IN_PROGRESS);
83ef82c0f827540ae1bbbff41542f02cba3f4e95vboxsync pReqInt->AioCB.aio_lio_opcode = uTransferDirection;
83ef82c0f827540ae1bbbff41542f02cba3f4e95vboxsyncRTDECL(int) RTFileAioReqPrepareRead(RTFILEAIOREQ hReq, RTFILE hFile, RTFOFF off,
83ef82c0f827540ae1bbbff41542f02cba3f4e95vboxsync return rtFileAioReqPrepareTransfer(hReq, hFile, LIO_READ,
83ef82c0f827540ae1bbbff41542f02cba3f4e95vboxsyncRTDECL(int) RTFileAioReqPrepareWrite(RTFILEAIOREQ hReq, RTFILE hFile, RTFOFF off,
83ef82c0f827540ae1bbbff41542f02cba3f4e95vboxsync return rtFileAioReqPrepareTransfer(hReq, hFile, LIO_WRITE,
83ef82c0f827540ae1bbbff41542f02cba3f4e95vboxsyncRTDECL(int) RTFileAioReqPrepareFlush(RTFILEAIOREQ hReq, RTFILE hFile, void *pvUser)
83ef82c0f827540ae1bbbff41542f02cba3f4e95vboxsync PRTFILEAIOREQINTERNAL pReqInt = (PRTFILEAIOREQINTERNAL)hReq;
83ef82c0f827540ae1bbbff41542f02cba3f4e95vboxsync RTFILEAIOREQ_NOT_STATE_RETURN_RC(pReqInt, SUBMITTED, VERR_FILE_AIO_IN_PROGRESS);
83ef82c0f827540ae1bbbff41542f02cba3f4e95vboxsyncRTDECL(void *) RTFileAioReqGetUser(RTFILEAIOREQ hReq)
83ef82c0f827540ae1bbbff41542f02cba3f4e95vboxsync RTFILEAIOREQ_STATE_RETURN_RC(pReqInt, SUBMITTED, VERR_FILE_AIO_NOT_SUBMITTED);
83ef82c0f827540ae1bbbff41542f02cba3f4e95vboxsync int rcSolaris = aio_cancel(pReqInt->AioCB.aio_fildes, &pReqInt->AioCB);
83ef82c0f827540ae1bbbff41542f02cba3f4e95vboxsync * Decrement request count because the request will never arrive at the
83ef82c0f827540ae1bbbff41542f02cba3f4e95vboxsync * completion port.
83ef82c0f827540ae1bbbff41542f02cba3f4e95vboxsync ("Invalid state. Request was canceled but wasn't submitted\n"));
83ef82c0f827540ae1bbbff41542f02cba3f4e95vboxsyncRTDECL(int) RTFileAioReqGetRC(RTFILEAIOREQ hReq, size_t *pcbTransfered)
83ef82c0f827540ae1bbbff41542f02cba3f4e95vboxsync RTFILEAIOREQ_NOT_STATE_RETURN_RC(pReqInt, SUBMITTED, VERR_FILE_AIO_IN_PROGRESS);
83ef82c0f827540ae1bbbff41542f02cba3f4e95vboxsync RTFILEAIOREQ_NOT_STATE_RETURN_RC(pReqInt, PREPARED, VERR_FILE_AIO_NOT_SUBMITTED);
83ef82c0f827540ae1bbbff41542f02cba3f4e95vboxsync Assert(rcSol != EINPROGRESS); /* Handled by our own state handling. */
83ef82c0f827540ae1bbbff41542f02cba3f4e95vboxsync /* An error occurred. */
83ef82c0f827540ae1bbbff41542f02cba3f4e95vboxsyncRTDECL(int) RTFileAioCtxCreate(PRTFILEAIOCTX phAioCtx, uint32_t cAioReqsMax)
83ef82c0f827540ae1bbbff41542f02cba3f4e95vboxsync pCtxInt = (PRTFILEAIOCTXINTERNAL)RTMemAllocZ(sizeof(RTFILEAIOCTXINTERNAL));
83ef82c0f827540ae1bbbff41542f02cba3f4e95vboxsync /* Init the event handle. */
83ef82c0f827540ae1bbbff41542f02cba3f4e95vboxsyncRTDECL(int) RTFileAioCtxDestroy(RTFILEAIOCTX hAioCtx)
83ef82c0f827540ae1bbbff41542f02cba3f4e95vboxsync /* Validate the handle and ignore nil. */
83ef82c0f827540ae1bbbff41542f02cba3f4e95vboxsync /* Cannot destroy a busy context. */
83ef82c0f827540ae1bbbff41542f02cba3f4e95vboxsync ASMAtomicUoWriteU32(&pCtxInt->u32Magic, RTFILEAIOCTX_MAGIC_DEAD);
83ef82c0f827540ae1bbbff41542f02cba3f4e95vboxsyncRTDECL(uint32_t) RTFileAioCtxGetMaxReqCount(RTFILEAIOCTX hAioCtx)
83ef82c0f827540ae1bbbff41542f02cba3f4e95vboxsyncRTDECL(int) RTFileAioCtxAssociateWithFile(RTFILEAIOCTX hAioCtx, RTFILE hFile)
83ef82c0f827540ae1bbbff41542f02cba3f4e95vboxsyncRTDECL(int) RTFileAioCtxSubmit(RTFILEAIOCTX hAioCtx, PRTFILEAIOREQ pahReqs, size_t cReqs)
83ef82c0f827540ae1bbbff41542f02cba3f4e95vboxsync * Parameter validation.
83ef82c0f827540ae1bbbff41542f02cba3f4e95vboxsync while(i-- > 0)
83ef82c0f827540ae1bbbff41542f02cba3f4e95vboxsync /* Undo everything and stop submitting. */
83ef82c0f827540ae1bbbff41542f02cba3f4e95vboxsync pReqInt->PortNotifier.portnfy_port = pCtxInt->iPort;
83ef82c0f827540ae1bbbff41542f02cba3f4e95vboxsync rcSol = lio_listio(LIO_NOWAIT, (struct aiocb **)pahReqs, cReqsSubmit, NULL);
83ef82c0f827540ae1bbbff41542f02cba3f4e95vboxsync /* Check which requests got actually submitted and which not. */
83ef82c0f827540ae1bbbff41542f02cba3f4e95vboxsync for (i = 0; i < cReqs; i++)
83ef82c0f827540ae1bbbff41542f02cba3f4e95vboxsync /* Was not submitted. */
83ef82c0f827540ae1bbbff41542f02cba3f4e95vboxsync /* The request encountered an error. */
83ef82c0f827540ae1bbbff41542f02cba3f4e95vboxsync * If there are still requests left we have a flush request.
83ef82c0f827540ae1bbbff41542f02cba3f4e95vboxsync * lio_listio does not work with this requests so
83ef82c0f827540ae1bbbff41542f02cba3f4e95vboxsync * we have to use aio_fsync directly.
b79e4344bf4eb8033fd06d560cd864192728bd0bvboxsyncRTDECL(int) RTFileAioCtxWait(RTFILEAIOCTX hAioCtx, size_t cMinReqs, RTMSINTERVAL cMillies,
83ef82c0f827540ae1bbbff41542f02cba3f4e95vboxsync PRTFILEAIOREQ pahReqs, size_t cReqs, uint32_t *pcReqs)
83ef82c0f827540ae1bbbff41542f02cba3f4e95vboxsync * Validate the parameters, making sure to always set pcReqs.
83ef82c0f827540ae1bbbff41542f02cba3f4e95vboxsync AssertReturn(cReqs >= cMinReqs, VERR_OUT_OF_RANGE);
83ef82c0f827540ae1bbbff41542f02cba3f4e95vboxsync if (RT_UNLIKELY(ASMAtomicReadS32(&pCtxInt->cRequests) == 0))
83ef82c0f827540ae1bbbff41542f02cba3f4e95vboxsync * Convert the timeout if specified.
83ef82c0f827540ae1bbbff41542f02cba3f4e95vboxsync /* Wait for at least one. */
83ef82c0f827540ae1bbbff41542f02cba3f4e95vboxsync port_event_t aPortEvents[AIO_MAXIMUM_REQUESTS_PER_CONTEXT];
83ef82c0f827540ae1bbbff41542f02cba3f4e95vboxsync int cRequestsToWait = cMinReqs < AIO_MAXIMUM_REQUESTS_PER_CONTEXT ? cReqs : AIO_MAXIMUM_REQUESTS_PER_CONTEXT;
83ef82c0f827540ae1bbbff41542f02cba3f4e95vboxsync rcSol = port_getn(pCtxInt->iPort, &aPortEvents[0], cRequestsToWait, &cRequests, pTimeout);
83ef82c0f827540ae1bbbff41542f02cba3f4e95vboxsync /* Process received events. */
83ef82c0f827540ae1bbbff41542f02cba3f4e95vboxsync if (aPortEvents[i].portev_source == PORT_SOURCE_ALERT)
83ef82c0f827540ae1bbbff41542f02cba3f4e95vboxsync Assert(aPortEvents[i].portev_events == AIO_CONTEXT_WAKEUP_EVENT);
83ef82c0f827540ae1bbbff41542f02cba3f4e95vboxsync rc = VERR_INTERRUPTED; /* We've got interrupted. */
83ef82c0f827540ae1bbbff41542f02cba3f4e95vboxsync /* Reset the port. */
83ef82c0f827540ae1bbbff41542f02cba3f4e95vboxsync port_alert(pCtxInt->iPort, PORT_ALERT_SET, 0, NULL);
83ef82c0f827540ae1bbbff41542f02cba3f4e95vboxsync PRTFILEAIOREQINTERNAL pReqInt = (PRTFILEAIOREQINTERNAL)aPortEvents[i].portev_user;
83ef82c0f827540ae1bbbff41542f02cba3f4e95vboxsync /* A request has finished. */
83ef82c0f827540ae1bbbff41542f02cba3f4e95vboxsync /* Mark the request as finished. */
83ef82c0f827540ae1bbbff41542f02cba3f4e95vboxsync * Done Yet? If not advance and try again.
83ef82c0f827540ae1bbbff41542f02cba3f4e95vboxsync uint64_t cMilliesElapsed = (NanoTS - StartNanoTS) / 1000000;
83ef82c0f827540ae1bbbff41542f02cba3f4e95vboxsync /* The syscall supposedly updates it, but we're paranoid. :-) */
b79e4344bf4eb8033fd06d560cd864192728bd0bvboxsync Timeout.tv_sec = (cMillies - (RTMSINTERVAL)cMilliesElapsed) / 1000;
b79e4344bf4eb8033fd06d560cd864192728bd0bvboxsync Timeout.tv_nsec = (cMillies - (RTMSINTERVAL)cMilliesElapsed) % 1000 * 1000000;
83ef82c0f827540ae1bbbff41542f02cba3f4e95vboxsync * Update the context state and set the return value.
83ef82c0f827540ae1bbbff41542f02cba3f4e95vboxsync ASMAtomicSubS32(&pCtxInt->cRequests, cRequestsCompleted);
83ef82c0f827540ae1bbbff41542f02cba3f4e95vboxsyncRTDECL(int) RTFileAioCtxWakeup(RTFILEAIOCTX hAioCtx)