fileaio-freebsd.cpp revision 22197ad5dba8323699e719b622fff028e69b9d54
/* $Id$ */
/** @file
* IPRT - File async I/O, native implementation for the FreeBSD host platform.
*/
/*
* Copyright (C) 2006-2007 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*
* The contents of this file may alternatively be used under the terms
* of the Common Development and Distribution License Version 1.0
* (CDDL) only, as it comes in the "COPYING.CDDL" file of the
* VirtualBox OSE distribution, in which case the provisions of the
* CDDL are applicable instead of those of the GPL.
*
* You may elect to license modified versions of this file under the
* terms and conditions of either the GPL or the CDDL or both.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#define LOG_GROUP RTLOGGROUP_FILE
#include <aio.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
/*******************************************************************************
* Structures and Typedefs *
*******************************************************************************/
/**
* Async I/O completion context state.
*/
typedef struct RTFILEAIOCTXINTERNAL
{
/** Handle to the kernel queue. */
int iKQueue;
/** Current number of requests active on this context. */
/** The ID of the thread which is currently waiting for requests. */
volatile RTTHREAD hThreadWait;
/** Flag whether the thread was woken up. */
volatile bool fWokenUp;
/** Flag whether the thread is currently waiting in the syscall. */
volatile bool fWaiting;
/** Magic value (RTFILEAIOCTX_MAGIC). */
/** Pointer to an internal context structure. */
typedef RTFILEAIOCTXINTERNAL *PRTFILEAIOCTXINTERNAL;
/**
* Async I/O request state.
*/
typedef struct RTFILEAIOREQINTERNAL
{
/** The aio control block. Must be the FIRST
* element. */
/** Current state the request is in. */
/** Flag whether this is a flush request. */
bool fFlush;
/** Opaque user data. */
void *pvUser;
/** Completion context we are assigned to. */
/** Number of bytes actually transferred. */
/** Status code. */
int Rc;
/** Magic value (RTFILEAIOREQ_MAGIC). */
/** Pointer to an internal request structure. */
typedef RTFILEAIOREQINTERNAL *PRTFILEAIOREQINTERNAL;
/*******************************************************************************
* Defined Constants And Macros *
*******************************************************************************/
/** The max number of events to get in one call. */
#define AIO_MAXIMUM_REQUESTS_PER_CONTEXT 64
{
int rcBSD = 0;
/*
* The AIO API is implemented in a kernel module which is not
* loaded by default.
* If it is loaded there are additional sysctl parameters.
*/
int cReqsOutstandingMax = 0;
size_t cbParameter = sizeof(int);
&cReqsOutstandingMax, /* Where to store the old value. */
&cbParameter, /* Size of the memory pointed to. */
NULL, /* Where the new value is located. */
NULL); /* Where the size of the new value is stored. */
if (rcBSD == -1)
{
/* ENOENT means the value is unknown thus the module is not loaded. */
return VERR_NOT_SUPPORTED;
else
return RTErrConvertFromErrno(errno);
}
pAioLimits->cbBufferAlignment = 0;
return VINF_SUCCESS;
}
{
if (RT_UNLIKELY(!pReqInt))
return VERR_NO_MEMORY;
/* Ininitialize static parts. */
return VINF_SUCCESS;
}
{
/*
* Validate the handle and ignore nil.
*/
if (hReq == NIL_RTFILEAIOREQ)
return VINF_SUCCESS;
/*
* Trash the magic and free it.
*/
return VINF_SUCCESS;
}
/**
* Worker setting up the request.
*/
unsigned uTransferDirection,
void *pvUser)
{
/*
* Validate the input.
*/
Assert(cbTransfer > 0);
return VINF_SUCCESS;
}
{
}
{
}
{
return VINF_SUCCESS;
}
{
}
{
if (rcBSD == AIO_CANCELED)
{
/*
* Decrement request count because the request will never arrive at the
* completion port.
*/
("Invalid state. Request was canceled but wasn't submitted\n"));
return VINF_SUCCESS;
}
else if (rcBSD == AIO_ALLDONE)
return VERR_FILE_AIO_COMPLETED;
else if (rcBSD == AIO_NOTCANCELED)
return VERR_FILE_AIO_IN_PROGRESS;
else
return RTErrConvertFromErrno(errno);
}
{
&& (pcbTransfered))
}
{
int rc = VINF_SUCCESS;
if (RT_UNLIKELY(!pCtxInt))
return VERR_NO_MEMORY;
/* Init the event handle. */
{
}
else
{
}
return rc;
}
{
/* Validate the handle and ignore nil. */
if (hAioCtx == NIL_RTFILEAIOCTX)
return VINF_SUCCESS;
/* Cannot destroy a busy context. */
return VERR_FILE_AIO_BUSY;
return VINF_SUCCESS;
}
{
return RTFILEAIO_UNLIMITED_REQS;
}
{
return VINF_SUCCESS;
}
{
/*
* Parameter validation.
*/
int rc = VINF_SUCCESS;
do
{
int rcBSD = 0;
size_t cReqsSubmit = 0;
size_t i = 0;
while ( (i < cReqs)
&& (i < AIO_LISTIO_MAX))
{
{
/* Undo everything and stop submitting. */
{
}
break;
}
break;
cReqsSubmit++;
i++;
}
if (cReqsSubmit)
{
if (RT_UNLIKELY(rcBSD < 0))
{
else
/* Check which requests got actually submitted and which not. */
for (i = 0; i < cReqs; i++)
{
{
/* Was not submitted. */
}
else if (rcBSD != EINPROGRESS)
{
/* The request encountered an error. */
pReqInt->cbTransfered = 0;
}
}
break;
}
cReqs -= cReqsSubmit;
pahReqs += cReqsSubmit;
}
/* Check if we have a flush request now. */
{
{
/*
* lio_listio does not work with flush requests so
* we have to use aio_fsync directly.
*/
if (RT_UNLIKELY(rcBSD < 0))
{
{
/* Was not submitted. */
}
else
{
pReqInt->cbTransfered = 0;
}
}
cReqs--;
pahReqs++;
}
}
} while (cReqs);
return rc;
}
{
int rc = VINF_SUCCESS;
int cRequestsCompleted = 0;
/*
* Validate the parameters, making sure to always set pcReqs.
*/
*pcReqs = 0; /* always set */
return VERR_FILE_AIO_NO_REQUEST;
/*
* Convert the timeout if specified.
*/
uint64_t StartNanoTS = 0;
if (cMillies != RT_INDEFINITE_WAIT)
{
StartNanoTS = RTTimeNanoTS();
}
/* Wait for at least one. */
if (!cMinReqs)
cMinReqs = 1;
/* For the wakeup call. */
while ( cMinReqs
&& RT_SUCCESS_NP(rc))
{
int cRequestsToWait = cMinReqs < AIO_MAXIMUM_REQUESTS_PER_CONTEXT ? cReqs : AIO_MAXIMUM_REQUESTS_PER_CONTEXT;
int rcBSD;
if (RT_UNLIKELY(rcBSD < 0))
{
break;
}
/* Process received events. */
{
/*
* Retrieve the status code here already because the
* user may omit the RTFileAioReqGetRC() call and
* we will leak kernel resources then.
* This will result in errors during submission
* of other requests as soon as the max_aio_queue_per_proc
* limit is reached.
*/
if (cbTransfered < 0)
{
pReqInt->cbTransfered = 0;
}
else
{
}
}
/*
* Done Yet? If not advance and try again.
*/
break;
if (cMillies != RT_INDEFINITE_WAIT)
{
/* The API doesn't return ETIMEDOUT, so we have to fix that ourselves. */
if (cMilliesElapsed >= cMillies)
{
rc = VERR_TIMEOUT;
break;
}
/* The syscall supposedly updates it, but we're paranoid. :-) */
}
}
/*
* Update the context state and set the return value.
*/
/*
* Clear the wakeup flag and set rc.
*/
&& RT_SUCCESS(rc))
{
}
return rc;
}
{
/** @todo r=bird: Define the protocol for how to resume work after calling
* this function. */
/*
* Read the thread handle before the status flag.
* If we read the handle after the flag we might
* end up with an invalid handle because the thread
* waiting in RTFileAioCtxWakeup() might get scheduled
* before we read the flag and returns.
* We can ensure that the handle is valid if fWaiting is true
* when reading the handle before the status flag.
*/
if ( !fWokenUp
&& fWaiting)
{
/*
* If a thread waits the handle must be valid.
* It is possible that the thread returns from
* kevent() before the signal is send.
* This is no problem because we already set fWokenUp
* to true which will let the thread return VERR_INTERRUPTED
* and the next call to RTFileAioCtxWait() will not
* return VERR_INTERRUPTED because signals are not saved
* and will simply vanish if the destination thread can't
* receive it.
*/
}
return VINF_SUCCESS;
}