2014-10-05 01:30:24 +02:00
|
|
|
/****************************************************************************
|
2014-10-06 16:10:32 +02:00
|
|
|
* fs/aio/aio_write.c
|
2014-10-05 01:30:24 +02:00
|
|
|
*
|
2017-09-30 01:48:15 +02:00
|
|
|
* Copyright (C) 2014, 2017 Gregory Nutt. All rights reserved.
|
2014-10-05 01:30:24 +02:00
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
* 3. Neither the name NuttX nor the names of its contributors may be
|
|
|
|
* used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
2014-10-06 18:53:25 +02:00
|
|
|
#include <stdarg.h>
|
2014-10-05 01:30:24 +02:00
|
|
|
#include <unistd.h>
|
2014-10-05 21:57:55 +02:00
|
|
|
#include <fcntl.h>
|
2014-10-05 14:53:56 +02:00
|
|
|
#include <sched.h>
|
2014-10-05 01:30:24 +02:00
|
|
|
#include <aio.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
2014-10-06 00:04:19 +02:00
|
|
|
#include <debug.h>
|
2014-10-05 01:30:24 +02:00
|
|
|
|
|
|
|
#include "aio/aio.h"
|
|
|
|
|
2014-10-05 23:44:43 +02:00
|
|
|
#ifdef CONFIG_FS_AIO
|
2014-10-05 01:30:24 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: aio_write_worker
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This function executes on the worker thread and performs the
|
|
|
|
* asynchronous I/O operation.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* arg - Worker argument. In this case, a pointer to an instance of
|
|
|
|
* struct aiocb cast to void *.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void aio_write_worker(FAR void *arg)
|
|
|
|
{
|
2014-10-06 16:10:32 +02:00
|
|
|
FAR struct aio_container_s *aioc = (FAR struct aio_container_s *)arg;
|
|
|
|
FAR struct aiocb *aiocbp;
|
|
|
|
pid_t pid;
|
2014-10-07 16:24:50 +02:00
|
|
|
#ifdef CONFIG_PRIORITY_INHERITANCE
|
|
|
|
uint8_t prio;
|
|
|
|
#endif
|
2014-11-25 20:46:14 +01:00
|
|
|
ssize_t nwritten = 0;
|
2014-10-05 01:30:24 +02:00
|
|
|
int oflags;
|
|
|
|
|
2014-10-06 16:10:32 +02:00
|
|
|
/* Get the information from the container, decant the AIO control block,
|
|
|
|
* and free the container before starting any I/O. That will minimize
|
|
|
|
* the delays by any other threads waiting for a pre-allocated container.
|
|
|
|
*/
|
|
|
|
|
|
|
|
DEBUGASSERT(aioc && aioc->aioc_aiocbp);
|
|
|
|
pid = aioc->aioc_pid;
|
2014-10-07 16:24:50 +02:00
|
|
|
#ifdef CONFIG_PRIORITY_INHERITANCE
|
|
|
|
prio = aioc->aioc_prio;
|
|
|
|
#endif
|
2014-10-06 16:10:32 +02:00
|
|
|
aiocbp = aioc_decant(aioc);
|
|
|
|
|
2019-02-11 19:09:26 +01:00
|
|
|
#ifdef AIO_HAVE_PSOCK
|
2015-02-24 14:29:47 +01:00
|
|
|
if (aiocbp->aio_fildes < CONFIG_NFILE_DESCRIPTORS)
|
2014-10-11 16:15:23 +02:00
|
|
|
#endif
|
2014-10-05 01:30:24 +02:00
|
|
|
{
|
2014-10-11 16:15:23 +02:00
|
|
|
/* Call fcntl(F_GETFL) to get the file open mode. */
|
|
|
|
|
|
|
|
oflags = file_fcntl(aioc->u.aioc_filep, F_GETFL);
|
|
|
|
if (oflags < 0)
|
|
|
|
{
|
2017-09-30 18:41:21 +02:00
|
|
|
ferr("ERROR: file_fcntl failed: %d\n", oflags);
|
|
|
|
aiocbp->aio_result = oflags;
|
2014-10-11 16:29:26 +02:00
|
|
|
goto errout;
|
2014-10-11 16:15:23 +02:00
|
|
|
}
|
|
|
|
|
2014-10-05 01:30:24 +02:00
|
|
|
/* Perform the write using:
|
|
|
|
*
|
2014-10-11 16:15:23 +02:00
|
|
|
* u.aioc_filep - File structure pointer
|
|
|
|
* aio_buf - Location of buffer
|
|
|
|
* aio_nbytes - Length of transfer
|
|
|
|
* aio_offset - File offset
|
2014-10-05 01:30:24 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* Check if O_APPEND is set in the file open flags */
|
|
|
|
|
|
|
|
if ((oflags & O_APPEND) != 0)
|
|
|
|
{
|
|
|
|
/* Append to the current file position */
|
|
|
|
|
2014-10-11 16:15:23 +02:00
|
|
|
nwritten = file_write(aioc->u.aioc_filep,
|
2014-10-06 18:53:25 +02:00
|
|
|
(FAR const void *)aiocbp->aio_buf,
|
|
|
|
aiocbp->aio_nbytes);
|
2014-10-05 01:30:24 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-10-11 16:15:23 +02:00
|
|
|
nwritten = file_pwrite(aioc->u.aioc_filep,
|
2014-10-06 18:53:25 +02:00
|
|
|
(FAR const void *)aiocbp->aio_buf,
|
|
|
|
aiocbp->aio_nbytes,
|
|
|
|
aiocbp->aio_offset);
|
2014-10-05 01:30:24 +02:00
|
|
|
}
|
2014-10-11 16:15:23 +02:00
|
|
|
}
|
|
|
|
#ifdef AIO_HAVE_PSOCK
|
2019-02-11 19:09:26 +01:00
|
|
|
else
|
2014-10-11 16:15:23 +02:00
|
|
|
{
|
|
|
|
/* Perform the send using:
|
|
|
|
*
|
|
|
|
* u.aioc_psock - Socket structure pointer
|
|
|
|
* aio_buf - Location of buffer
|
|
|
|
* aio_nbytes - Length of transfer
|
|
|
|
*/
|
2014-10-05 01:30:24 +02:00
|
|
|
|
2014-10-11 16:15:23 +02:00
|
|
|
nwritten = psock_send(aioc->u.aioc_psock,
|
|
|
|
(FAR const void *)aiocbp->aio_buf,
|
|
|
|
aiocbp->aio_nbytes, 0);
|
|
|
|
}
|
|
|
|
#endif
|
2014-10-05 01:30:24 +02:00
|
|
|
|
2017-09-30 01:48:15 +02:00
|
|
|
if (nwritten < 0)
|
|
|
|
{
|
|
|
|
ferr("ERROR: write/pwrite/send failed: %d\n", nwritten);
|
|
|
|
}
|
|
|
|
|
2017-09-28 22:49:05 +02:00
|
|
|
/* Save the result of the write */
|
2014-10-11 16:15:23 +02:00
|
|
|
|
2017-09-28 22:49:05 +02:00
|
|
|
aiocbp->aio_result = nwritten;
|
2014-10-11 16:15:23 +02:00
|
|
|
|
|
|
|
errout:
|
2014-10-05 01:30:24 +02:00
|
|
|
|
|
|
|
/* Signal the client */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
aio_signal(pid, aiocbp);
|
2014-10-07 16:24:50 +02:00
|
|
|
|
|
|
|
#ifdef CONFIG_PRIORITY_INHERITANCE
|
|
|
|
/* Restore the low priority worker thread default priority */
|
|
|
|
|
|
|
|
lpwork_restorepriority(prio);
|
|
|
|
#endif
|
2014-10-05 01:30:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: aio_write
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* The aio_write() function writes aiocbp->aio_nbytes to the file
|
|
|
|
* associated with aiocbp->aio_fildes from the buffer pointed to by
|
|
|
|
* aiocbp->aio_buf. The function call will return when the write request
|
|
|
|
* has been initiated or queued to the file or device (even when the data
|
|
|
|
* cannot be delivered immediately).
|
|
|
|
*
|
2014-10-07 16:34:10 +02:00
|
|
|
* If prioritized I/O is supported for this file, then the asynchronous
|
|
|
|
* operation will be submitted at a priority equal to a base scheduling
|
|
|
|
* priority minus aiocbp->aio_reqprio. If Thread Execution Scheduling is
|
|
|
|
* not supported, then the base scheduling priority is that of the calling
|
|
|
|
* thread (the latter is implemented at present).
|
|
|
|
*
|
2014-10-05 01:30:24 +02:00
|
|
|
* The aiocbp value may be used as an argument to aio_error() and
|
|
|
|
* aio_return() in order to determine the error status and return status,
|
|
|
|
* respectively, of the asynchronous operation while it is proceeding.
|
|
|
|
*
|
|
|
|
* The aiocbp argument points to an aiocb structure. If the buffer pointed
|
|
|
|
* to by aiocbp->aio_buf or the control block pointed to by aiocbp becomes
|
|
|
|
* an illegal address prior to asynchronous I/O completion, then the
|
|
|
|
* behavior is undefined.
|
|
|
|
*
|
|
|
|
* If O_APPEND is not set for the file descriptor aio_fildes, then the
|
|
|
|
* requested operation will take place at the absolute position in the file
|
2020-03-09 15:21:22 +01:00
|
|
|
* as given by aio_offset, as if lseek() were called immediately prior to
|
|
|
|
* the operation with an offset equal to aio_offset and a whence equal to
|
|
|
|
* SEEK_SET. If O_APPEND is set for the file descriptor, write operations
|
|
|
|
* append to the file in the same order as the calls were made. After a
|
|
|
|
* successful call to enqueue an asynchronous I/O operation, the value of
|
|
|
|
* the file offset for the file is unspecified.
|
2014-10-05 01:30:24 +02:00
|
|
|
*
|
|
|
|
* The aiocbp->aio_lio_opcode field will be ignored by aio_write().
|
|
|
|
*
|
|
|
|
* Simultaneous asynchronous operations using the same aiocbp produce
|
|
|
|
* undefined results.
|
|
|
|
*
|
|
|
|
* For any system action that changes the process memory space while an
|
|
|
|
* synchronous I/O is outstanding to the address range being changed, the
|
|
|
|
* result of that action is undefined.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* aiocbp - A pointer to an instance of struct aiocb
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
*
|
|
|
|
* The aio_write() function will return the value zero if the I/O operation
|
|
|
|
* is successfully queued; otherwise, the function will return the value
|
|
|
|
* -1 and set errno to indicate the error. The aio_write() function will
|
|
|
|
* ail if:
|
|
|
|
*
|
|
|
|
* EAGAIN - The requested asynchronous I/O operation was not queued due to
|
|
|
|
* system resource limitations.
|
|
|
|
*
|
|
|
|
* Each of the following conditions may be detected synchronously at the
|
|
|
|
* time of the call to aio_write(), or asynchronously. If any of the
|
|
|
|
* conditions below are detected synchronously, the aio_write() function
|
|
|
|
* will return -1 and set errno to the corresponding value. If any of the
|
|
|
|
* conditions below are detected asynchronously, the return status of the
|
|
|
|
* asynchronous operation is set to -1, and the error status of the
|
|
|
|
* asynchronous operation is set to the corresponding value.
|
|
|
|
*
|
|
|
|
* EBADF - The aiocbp->aio_fildes argument is not a valid file descriptor
|
|
|
|
* open for writing.
|
|
|
|
* EINVAL - The file offset value implied by aiocbp->aio_offset would be
|
|
|
|
* invalid, aiocbp->aio_reqprio is not a valid value, or
|
|
|
|
* aiocbp->aio_nbytes is an invalid value.
|
|
|
|
*
|
|
|
|
* In the case that the aio_write() successfully queues the I/O operation
|
2016-12-10 01:39:40 +01:00
|
|
|
* but the operation is subsequently canceled or encounters an error, the
|
2014-10-05 01:30:24 +02:00
|
|
|
* return status of the asynchronous operation is one of the values
|
|
|
|
* normally returned by the write() function call. In addition, the error
|
|
|
|
* status of the asynchronous operation is set to one of the error
|
|
|
|
* statuses normally set by the write() function call, or one of the
|
|
|
|
* following values:
|
|
|
|
*
|
|
|
|
* EBADF - The aiocbp->aio_fildes argument is not a valid file descriptor
|
|
|
|
* open for writing.
|
|
|
|
* EINVAL - The file offset value implied by aiocbp->aio_offset would be
|
|
|
|
* invalid.
|
2016-12-10 01:39:40 +01:00
|
|
|
* ECANCELED - The requested I/O was canceled before the I/O completed
|
2014-10-05 01:30:24 +02:00
|
|
|
* due to an explicit aio_cancel() request.
|
|
|
|
*
|
|
|
|
* The following condition may be detected synchronously or asynchronously:
|
|
|
|
*
|
|
|
|
* EFBIG - The file is a regular file, aiobcp->aio_nbytes is greater
|
|
|
|
* than 0, and the starting offset in aiobcp->aio_offset is at or
|
|
|
|
* beyond the offset maximum in the open file description associated
|
|
|
|
* with aiocbp->aio_fildes.
|
|
|
|
*
|
|
|
|
* POSIX Compliance:
|
|
|
|
* - Most errors required in the standard are not detected at this point.
|
|
|
|
* There are no pre-queuing checks for the validity of the operation.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-10-05 14:53:56 +02:00
|
|
|
int aio_write(FAR struct aiocb *aiocbp)
|
2014-10-05 01:30:24 +02:00
|
|
|
{
|
2014-10-06 16:10:32 +02:00
|
|
|
FAR struct aio_container_s *aioc;
|
2014-10-05 01:30:24 +02:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
DEBUGASSERT(aiocbp);
|
|
|
|
|
2014-10-05 02:17:54 +02:00
|
|
|
/* The result -EINPROGRESS means that the transfer has not yet completed */
|
2014-10-05 01:30:24 +02:00
|
|
|
|
2019-01-27 16:28:59 +01:00
|
|
|
sigwork_init(&aiocbp->aio_sigwork);
|
2014-10-05 02:17:54 +02:00
|
|
|
aiocbp->aio_result = -EINPROGRESS;
|
2014-10-05 20:34:22 +02:00
|
|
|
aiocbp->aio_priv = NULL;
|
2014-10-05 01:30:24 +02:00
|
|
|
|
2014-10-06 18:53:25 +02:00
|
|
|
/* Create a container for the AIO control block. This may cause us to
|
|
|
|
* block if there are insufficient resources to satisfy the request.
|
2014-10-06 16:10:32 +02:00
|
|
|
*/
|
2014-10-05 01:30:24 +02:00
|
|
|
|
2014-10-06 16:10:32 +02:00
|
|
|
aioc = aio_contain(aiocbp);
|
2014-10-06 18:53:25 +02:00
|
|
|
if (!aioc)
|
|
|
|
{
|
|
|
|
/* The errno has already been set (probably EBADF) */
|
|
|
|
|
|
|
|
aiocbp->aio_result = -get_errno();
|
|
|
|
return ERROR;
|
|
|
|
}
|
2014-10-05 01:30:24 +02:00
|
|
|
|
|
|
|
/* Defer the work to the worker thread */
|
|
|
|
|
2014-10-07 16:24:50 +02:00
|
|
|
ret = aio_queue(aioc, aio_write_worker);
|
2014-10-05 01:30:24 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2014-10-07 16:24:50 +02:00
|
|
|
/* The result and the errno have already been set */
|
|
|
|
|
2019-01-27 16:39:33 +01:00
|
|
|
aioc_decant(aioc);
|
2014-10-05 01:30:24 +02:00
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2014-10-05 23:44:43 +02:00
|
|
|
#endif /* CONFIG_FS_AIO */
|