2014-10-05 04:22:00 +08:00
|
|
|
/****************************************************************************
|
2014-10-06 22:10:32 +08:00
|
|
|
* fs/aio/aio_read.c
|
2014-10-05 04:22:00 +08:00
|
|
|
*
|
2021-02-03 21:47:23 +08:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2014-10-05 04:22:00 +08:00
|
|
|
*
|
2021-02-03 21:47:23 +08:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2014-10-05 04:22:00 +08:00
|
|
|
*
|
2021-02-03 21:47:23 +08:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2014-10-05 04:22:00 +08:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
2014-10-05 04:44:57 +08:00
|
|
|
#include <unistd.h>
|
2014-10-05 20:53:56 +08:00
|
|
|
#include <sched.h>
|
2014-10-05 04:22:00 +08:00
|
|
|
#include <aio.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
2014-10-06 06:04:19 +08:00
|
|
|
#include <debug.h>
|
2014-10-05 04:22:00 +08:00
|
|
|
|
2021-10-30 21:39:46 +08:00
|
|
|
#include <nuttx/fs/fs.h>
|
2014-10-11 22:15:23 +08:00
|
|
|
|
2014-10-05 04:22:00 +08:00
|
|
|
#include "aio/aio.h"
|
|
|
|
|
2014-10-06 05:44:43 +08:00
|
|
|
#ifdef CONFIG_FS_AIO
|
2014-10-05 04:22:00 +08:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: aio_read_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_read_worker(FAR void *arg)
|
|
|
|
{
|
2014-10-06 22:10:32 +08:00
|
|
|
FAR struct aio_container_s *aioc = (FAR struct aio_container_s *)arg;
|
|
|
|
FAR struct aiocb *aiocbp;
|
|
|
|
pid_t pid;
|
2014-10-07 22:24:50 +08:00
|
|
|
#ifdef CONFIG_PRIORITY_INHERITANCE
|
|
|
|
uint8_t prio;
|
|
|
|
#endif
|
2014-11-26 03:46:14 +08:00
|
|
|
ssize_t nread = 0;
|
2014-10-05 04:44:57 +08:00
|
|
|
|
2014-10-06 22:10:32 +08: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 22:24:50 +08:00
|
|
|
#ifdef CONFIG_PRIORITY_INHERITANCE
|
|
|
|
prio = aioc->aioc_prio;
|
|
|
|
#endif
|
2014-10-06 22:10:32 +08:00
|
|
|
aiocbp = aioc_decant(aioc);
|
|
|
|
|
2021-03-08 14:49:18 +08:00
|
|
|
/* Perform the file read using:
|
|
|
|
*
|
|
|
|
* aioc_filep - File structure pointer
|
|
|
|
* aio_buf - Location of buffer
|
|
|
|
* aio_nbytes - Length of transfer
|
|
|
|
* aio_offset - File offset
|
|
|
|
*/
|
2014-10-11 22:15:23 +08:00
|
|
|
|
2021-03-08 14:49:18 +08:00
|
|
|
nread = file_pread(aioc->aioc_filep, (FAR void *)aiocbp->aio_buf,
|
|
|
|
aiocbp->aio_nbytes, aiocbp->aio_offset);
|
2014-10-05 04:44:57 +08:00
|
|
|
|
2018-01-31 23:19:19 +08:00
|
|
|
/* Set the result of the read operation. */
|
2014-10-05 04:44:57 +08:00
|
|
|
|
2018-01-31 23:19:19 +08:00
|
|
|
#ifdef CONFIG_DEBUG_FS_ERROR
|
2014-10-05 04:44:57 +08:00
|
|
|
if (nread < 0)
|
|
|
|
{
|
2018-01-31 23:19:19 +08:00
|
|
|
ferr("ERROR: read failed: %d\n", (int)nread);
|
2014-10-05 04:44:57 +08:00
|
|
|
}
|
2018-01-31 23:19:19 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
aiocbp->aio_result = nread;
|
2014-10-05 04:44:57 +08:00
|
|
|
|
|
|
|
/* Signal the client */
|
|
|
|
|
2020-01-03 00:49:34 +08:00
|
|
|
aio_signal(pid, aiocbp);
|
2014-10-07 22:24:50 +08:00
|
|
|
|
|
|
|
#ifdef CONFIG_PRIORITY_INHERITANCE
|
|
|
|
/* Restore the low priority worker thread default priority */
|
|
|
|
|
|
|
|
lpwork_restorepriority(prio);
|
|
|
|
#endif
|
2014-10-05 04:22:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: aio_read
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* The aio_read() function reads aiocbp->aio_nbytes from the file
|
|
|
|
* associated with aiocbp->aio_fildes into the buffer pointed to by
|
|
|
|
* aiocbp->aio_buf. The function call will return when the read request
|
|
|
|
* has been initiated or queued to the file or device (even when the data
|
|
|
|
* cannot be delivered immediately).
|
|
|
|
*
|
2014-10-07 22:34:10 +08: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 04:22:00 +08: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. If
|
|
|
|
* an error condition is encountered during queuing, the function call
|
|
|
|
* will return without having initiated or queued the request. The
|
|
|
|
* requested operation takes place at the absolute position in the file 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. After a successful call to enqueue an asynchronous I/O
|
|
|
|
* operation, the value of the file offset for the file is unspecified.
|
|
|
|
*
|
|
|
|
* The aiocbp->aio_lio_opcode field will be ignored by aio_read().
|
2014-10-05 07:30:24 +08:00
|
|
|
*
|
2014-10-05 04:22:00 +08:00
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* Simultaneous asynchronous operations using the same aiocbp produce
|
|
|
|
* undefined results.
|
|
|
|
*
|
2014-10-05 07:30:24 +08:00
|
|
|
* For any system action that changes the process memory space while an
|
2014-10-05 04:22:00 +08:00
|
|
|
* 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_read() 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_read() function will
|
2022-08-26 10:56:51 +08:00
|
|
|
* fail if:
|
2014-10-05 04:22:00 +08:00
|
|
|
*
|
|
|
|
* 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_read(), or asynchronously. If any of the
|
|
|
|
* conditions below are detected synchronously, the aio_read() 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 reading.
|
|
|
|
* 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_read() successfully queues the I/O operation
|
2016-12-10 08:39:40 +08:00
|
|
|
* but the operation is subsequently canceled or encounters an error, the
|
2014-10-05 04:22:00 +08:00
|
|
|
* return status of the asynchronous operation is one of the values
|
|
|
|
* normally returned by the read() function call. In addition, the error
|
|
|
|
* status of the asynchronous operation is set to one of the error
|
|
|
|
* statuses normally set by the read() function call, or one of the
|
|
|
|
* following values:
|
|
|
|
*
|
|
|
|
* EBADF - The aiocbp->aio_fildes argument is not a valid file descriptor
|
|
|
|
* open for reading.
|
2016-12-10 08:39:40 +08:00
|
|
|
* ECANCELED - The requested I/O was canceled before the I/O completed
|
2014-10-05 04:22:00 +08:00
|
|
|
* due to an explicit aio_cancel() request.
|
|
|
|
* EINVAL - The file offset value implied by aiocbp->aio_offset would be
|
|
|
|
* invalid.
|
|
|
|
*
|
|
|
|
* The following condition may be detected synchronously or asynchronously:
|
|
|
|
*
|
|
|
|
* EOVERFLOW - The file is a regular file, aiobcp->aio_nbytes is greater
|
|
|
|
* than 0, and the starting offset in aiobcp->aio_offset is before the
|
|
|
|
* end-of-file and 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 20:53:56 +08:00
|
|
|
int aio_read(FAR struct aiocb *aiocbp)
|
2014-10-05 04:22:00 +08:00
|
|
|
{
|
2014-10-06 22:10:32 +08:00
|
|
|
FAR struct aio_container_s *aioc;
|
2014-10-05 04:22:00 +08:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
DEBUGASSERT(aiocbp);
|
|
|
|
|
2014-10-05 08:17:54 +08:00
|
|
|
/* The result -EINPROGRESS means that the transfer has not yet completed */
|
2014-10-05 04:44:57 +08:00
|
|
|
|
2019-01-27 23:28:59 +08:00
|
|
|
sigwork_init(&aiocbp->aio_sigwork);
|
2014-10-05 08:17:54 +08:00
|
|
|
aiocbp->aio_result = -EINPROGRESS;
|
2014-10-06 02:34:22 +08:00
|
|
|
aiocbp->aio_priv = NULL;
|
2014-10-05 04:44:57 +08:00
|
|
|
|
2014-10-07 00:53:25 +08: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 22:10:32 +08:00
|
|
|
*/
|
2014-10-05 04:44:57 +08:00
|
|
|
|
2014-10-06 22:10:32 +08:00
|
|
|
aioc = aio_contain(aiocbp);
|
2014-10-07 00:53:25 +08:00
|
|
|
if (!aioc)
|
|
|
|
{
|
|
|
|
/* The errno has already been set (probably EBADF) */
|
|
|
|
|
|
|
|
aiocbp->aio_result = -get_errno();
|
|
|
|
return ERROR;
|
|
|
|
}
|
2014-10-05 04:44:57 +08:00
|
|
|
|
2014-10-05 04:22:00 +08:00
|
|
|
/* Defer the work to the worker thread */
|
|
|
|
|
2014-10-07 22:24:50 +08:00
|
|
|
ret = aio_queue(aioc, aio_read_worker);
|
2014-10-05 04:22:00 +08:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2014-10-07 22:24:50 +08:00
|
|
|
/* The result and the errno have already been set */
|
|
|
|
|
2019-01-27 23:39:33 +08:00
|
|
|
aioc_decant(aioc);
|
2014-10-05 04:22:00 +08:00
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2014-10-06 05:44:43 +08:00
|
|
|
#endif /* CONFIG_FS_AIO */
|