2011-05-07 22:08:46 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* fs/mmap/fs_rammmap.c
|
|
|
|
*
|
|
|
|
* Copyright (C) 2011 Gregory Nutt. All rights reserved.
|
2012-09-13 20:32:24 +02:00
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
2011-05-07 22:08:46 +02:00
|
|
|
*
|
|
|
|
* 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>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
2011-05-08 16:42:42 +02:00
|
|
|
#include <sys/mman.h>
|
2011-05-08 02:20:44 +02:00
|
|
|
|
2011-05-08 16:42:42 +02:00
|
|
|
#include <string.h>
|
2011-05-08 02:20:44 +02:00
|
|
|
#include <unistd.h>
|
2011-05-07 22:08:46 +02:00
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
2011-05-08 16:42:42 +02:00
|
|
|
#include <nuttx/kmalloc.h>
|
|
|
|
|
2014-09-29 15:14:38 +02:00
|
|
|
#include "inode/inode.h"
|
2011-05-08 16:42:42 +02:00
|
|
|
#include "fs_rammap.h"
|
2011-05-07 22:08:46 +02:00
|
|
|
|
|
|
|
#ifdef CONFIG_FS_RAMMAP
|
|
|
|
|
2011-05-08 16:42:42 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Global Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* This is the list of all mapped files */
|
|
|
|
|
|
|
|
struct fs_allmaps_s g_rammaps;
|
|
|
|
|
2011-05-07 22:08:46 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Global Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2011-05-08 16:42:42 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: rammap_initialize
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Verified that this capability has been initialized.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
void rammap_initialize(void)
|
|
|
|
{
|
|
|
|
if (!g_rammaps.initialized)
|
|
|
|
{
|
|
|
|
sem_init(&g_rammaps.exclsem, 0, 1);
|
|
|
|
g_rammaps.initialized = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-07 22:08:46 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: rammmap
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Support simulation of memory mapped files by copying files into RAM.
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* fd file descriptor of the backing file -- required.
|
|
|
|
* length The length of the mapping. For exception #1 above, this length
|
|
|
|
* ignored: The entire underlying media is always accessible.
|
|
|
|
* offset The offset into the file to map
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* On success, rammmap() returns a pointer to the mapped area. On error, the
|
|
|
|
* value MAP_FAILED is returned, and errno is set appropriately.
|
|
|
|
*
|
|
|
|
* EBADF
|
|
|
|
* 'fd' is not a valid file descriptor.
|
|
|
|
* EINVAL
|
|
|
|
* 'length' or 'offset' are invalid
|
|
|
|
* ENOMEM
|
|
|
|
* Insufficient memory is available to map the file.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2011-05-08 16:42:42 +02:00
|
|
|
FAR void *rammap(int fd, size_t length, off_t offset)
|
2011-05-07 22:08:46 +02:00
|
|
|
{
|
2011-05-08 16:42:42 +02:00
|
|
|
FAR struct fs_rammap_s *map;
|
2011-05-08 02:20:44 +02:00
|
|
|
FAR uint8_t *alloc;
|
|
|
|
FAR uint8_t *rdbuffer;
|
|
|
|
ssize_t nread;
|
|
|
|
off_t fpos;
|
|
|
|
int err;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* There is a major design flaw that I have not yet thought of fix for:
|
|
|
|
* The goal is to have a single region of memory that represents a single
|
|
|
|
* file and can be shared by many threads. That is, given a filename a
|
|
|
|
* thread should be able to open the file, get a file descriptor, and
|
|
|
|
* call mmap() to get a memory region. Different file descriptors opened
|
|
|
|
* with the same file path should get the same memory region when mapped.
|
|
|
|
*
|
|
|
|
* The design flaw is that I don't have sufficient knowledge to know that
|
|
|
|
* these different file descriptors map to the same file. So, for the time
|
|
|
|
* being, a new memory region is created each time that rammap() is called.
|
|
|
|
* Not very useful!
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Allocate a region of memory of the specified size */
|
|
|
|
|
2014-09-01 00:24:24 +02:00
|
|
|
alloc = (FAR uint8_t *)kumm_malloc(sizeof(struct fs_rammap_s) + length);
|
2011-05-08 02:20:44 +02:00
|
|
|
if (!alloc)
|
|
|
|
{
|
|
|
|
fdbg("Region allocation failed, length: %d\n", (int)length);
|
|
|
|
err = ENOMEM;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize the region */
|
|
|
|
|
2011-05-08 16:42:42 +02:00
|
|
|
map = (FAR struct fs_rammap_s *)alloc;
|
|
|
|
memset(map, 0, sizeof(struct fs_rammap_s));
|
|
|
|
map->addr = alloc + sizeof(struct fs_rammap_s);
|
|
|
|
map->length = length;
|
|
|
|
map->offset = offset;
|
2011-05-08 02:20:44 +02:00
|
|
|
|
|
|
|
/* Seek to the specified file offset */
|
|
|
|
|
|
|
|
fpos = lseek(fd, offset, SEEK_SET);
|
|
|
|
if (fpos == (off_t)-1)
|
|
|
|
{
|
|
|
|
/* Seek failed... errno has already been set, but EINVAL is probably
|
|
|
|
* the correct response.
|
|
|
|
*/
|
|
|
|
|
|
|
|
fdbg("Seek to position %d failed\n", (int)offset);
|
2012-09-10 22:00:04 +02:00
|
|
|
err = EINVAL;
|
2011-05-08 02:20:44 +02:00
|
|
|
goto errout_with_region;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read the file data into the memory region */
|
|
|
|
|
2011-05-08 16:42:42 +02:00
|
|
|
rdbuffer = map->addr;
|
2011-05-08 02:20:44 +02:00
|
|
|
while (length > 0)
|
|
|
|
{
|
|
|
|
nread = read(fd, rdbuffer, length);
|
|
|
|
if (nread < 0)
|
|
|
|
{
|
|
|
|
/* Handle the special case where the read was interrupted by a
|
|
|
|
* signal.
|
|
|
|
*/
|
|
|
|
|
2012-09-10 22:00:04 +02:00
|
|
|
err = get_errno();
|
|
|
|
if (err != EINTR)
|
2011-05-08 02:20:44 +02:00
|
|
|
{
|
|
|
|
/* All other read errors are bad. errno is already set.
|
2012-09-10 22:00:04 +02:00
|
|
|
* (but maybe should be forced to EINVAL?). NOTE that if
|
|
|
|
* FS DEBUG is enabled, then the following fdbg() macro will
|
|
|
|
* destroy the errno value.
|
2011-05-08 02:20:44 +02:00
|
|
|
*/
|
|
|
|
|
2012-09-10 22:00:04 +02:00
|
|
|
fdbg("Read failed: offset=%d errno=%d\n", (int)offset, err);
|
|
|
|
#ifdef CONFIG_DEBUG_FS
|
|
|
|
goto errout_with_region;
|
|
|
|
#else
|
2011-05-08 02:20:44 +02:00
|
|
|
goto errout_with_errno;
|
2012-09-10 22:00:04 +02:00
|
|
|
#endif
|
2011-05-08 02:20:44 +02:00
|
|
|
}
|
2012-09-10 22:00:04 +02:00
|
|
|
}
|
2011-05-08 02:20:44 +02:00
|
|
|
|
2012-09-10 22:00:04 +02:00
|
|
|
/* Check for end of file. */
|
2011-05-08 02:20:44 +02:00
|
|
|
|
2012-09-10 22:00:04 +02:00
|
|
|
if (nread == 0)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2011-05-08 02:20:44 +02:00
|
|
|
|
2012-09-10 22:00:04 +02:00
|
|
|
/* Increment number of bytes read */
|
2011-05-08 02:20:44 +02:00
|
|
|
|
2012-09-10 22:00:04 +02:00
|
|
|
rdbuffer += nread;
|
|
|
|
length -= nread;
|
2011-05-08 02:20:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Zero any memory beyond the amount read from the file */
|
|
|
|
|
|
|
|
memset(rdbuffer, 0, length);
|
|
|
|
|
|
|
|
/* Add the buffer to the list of regions */
|
|
|
|
|
2011-05-08 16:42:42 +02:00
|
|
|
rammap_initialize();
|
|
|
|
ret = sem_wait(&g_rammaps.exclsem);
|
2011-05-08 02:20:44 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
goto errout_with_errno;
|
|
|
|
}
|
|
|
|
|
2011-05-08 16:42:42 +02:00
|
|
|
map->flink = g_rammaps.head;
|
|
|
|
g_rammaps.head = map;
|
2011-05-08 02:20:44 +02:00
|
|
|
|
2011-05-08 16:42:42 +02:00
|
|
|
sem_post(&g_rammaps.exclsem);
|
|
|
|
return map->addr;
|
2011-05-08 02:20:44 +02:00
|
|
|
|
|
|
|
errout_with_region:
|
2014-09-01 00:15:11 +02:00
|
|
|
kumm_free(alloc);
|
2011-05-08 02:20:44 +02:00
|
|
|
errout:
|
2012-09-10 22:00:04 +02:00
|
|
|
set_errno(err);
|
2011-05-08 02:20:44 +02:00
|
|
|
return MAP_FAILED;
|
|
|
|
|
|
|
|
errout_with_errno:
|
2014-09-01 00:15:11 +02:00
|
|
|
kumm_free(alloc);
|
2011-05-08 16:42:42 +02:00
|
|
|
return MAP_FAILED;
|
2011-05-07 22:08:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* CONFIG_FS_RAMMAP */
|