fs/mmap: Remove the duplication rammap handling

and minor ifx for style and format

Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
This commit is contained in:
Xiang Xiao
2023-01-11 00:48:45 +08:00
committed by Petro Karashchenko
parent 120594079a
commit ab67d6a75b
3 changed files with 18 additions and 42 deletions
+6 -31
View File
@@ -85,15 +85,6 @@ static int file_mmap_(FAR struct file *filep, FAR void *start,
return -ENOSYS; return -ENOSYS;
} }
#ifndef CONFIG_FS_RAMMAP
if ((flags & MAP_PRIVATE) != 0)
{
ferr("ERROR: MAP_PRIVATE is not supported without file mapping"
"emulation\n");
return -ENOSYS;
}
#endif /* CONFIG_FS_RAMMAP */
/* A length of 0 is invalid. */ /* A length of 0 is invalid. */
if (length == 0) if (length == 0)
@@ -103,8 +94,7 @@ static int file_mmap_(FAR struct file *filep, FAR void *start,
} }
#endif /* CONFIG_DEBUG_FEATURES */ #endif /* CONFIG_DEBUG_FEATURES */
if ((filep->f_oflags & O_WROK) == 0 && prot == PROT_WRITE && if ((filep->f_oflags & O_WROK) == 0 && prot == PROT_WRITE)
(flags & MAP_SHARED) != 0)
{ {
ferr("ERROR: Unsupported options for read-only file descriptor," ferr("ERROR: Unsupported options for read-only file descriptor,"
"prot=%x flags=%04x\n", prot, flags); "prot=%x flags=%04x\n", prot, flags);
@@ -128,45 +118,30 @@ static int file_mmap_(FAR struct file *filep, FAR void *start,
return map_anonymous(&entry, kernel); return map_anonymous(&entry, kernel);
} }
if ((flags & MAP_PRIVATE) != 0)
{
#ifdef CONFIG_FS_RAMMAP
/* Allocate memory and copy the file into memory. We would, of course,
* do much better in the KERNEL build using the MMU.
*/
return rammap(filep, &entry, kernel);
#endif
}
/* Call driver's mmap to get the base address of the file in 'mapped' /* Call driver's mmap to get the base address of the file in 'mapped'
* in memory. * in memory.
*/ */
if (filep->f_inode && filep->f_inode->u.i_ops->mmap != NULL) if ((flags & MAP_PRIVATE) == 0 && filep->f_inode &&
filep->f_inode->u.i_ops->mmap != NULL)
{ {
ret = filep->f_inode->u.i_ops->mmap(filep, &entry); ret = filep->f_inode->u.i_ops->mmap(filep, &entry);
if (ret == OK) if (ret == OK)
{ {
*mapped = (void *)entry.vaddr; *mapped = entry.vaddr;
} }
} }
else else
{ {
/* Not directly mappable, probably because the underlying media does /* Caller request the private mapping. Or not directly mappable,
* not support random access. * probably because the underlying media doesn't support random access.
*/ */
#ifdef CONFIG_FS_RAMMAP
/* Allocate memory and copy the file into memory. We would, of course, /* Allocate memory and copy the file into memory. We would, of course,
* do much better in the KERNEL build using the MMU. * do much better in the KERNEL build using the MMU.
*/ */
return rammap(filep, &entry, kernel); return rammap(filep, &entry, kernel);
#else
ferr("ERROR: mmap not supported \n");
return -ENOSYS;
#endif
} }
/* Return */ /* Return */
+9 -10
View File
@@ -51,9 +51,9 @@ static int unmap_rammap(FAR struct task_group_s *group,
size_t length) size_t length)
{ {
FAR void *newaddr; FAR void *newaddr;
unsigned int offset; off_t offset;
bool kernel = entry->priv.i != 0 ? true : false; bool kernel = entry->priv.i;
int ret; int ret = OK;
/* Get the offset from the beginning of the region and the actual number /* Get the offset from the beginning of the region and the actual number
* of bytes to "unmap". All mappings must extend to the end of the region. * of bytes to "unmap". All mappings must extend to the end of the region.
@@ -111,9 +111,8 @@ static int unmap_rammap(FAR struct task_group_s *group,
} }
DEBUGASSERT(newaddr == entry->vaddr); DEBUGASSERT(newaddr == entry->vaddr);
UNUSED(newaddr); /* May not be used */ entry->vaddr = newaddr;
entry->length = length; entry->length = length;
ret = OK;
} }
return ret; return ret;
@@ -176,7 +175,7 @@ int rammap(FAR struct file *filep, FAR struct mm_map_entry_s *entry,
rdbuffer = kernel ? kmm_malloc(length) : kumm_malloc(length); rdbuffer = kernel ? kmm_malloc(length) : kumm_malloc(length);
if (!rdbuffer) if (!rdbuffer)
{ {
ferr("ERROR: Region allocation failed, length: %d\n", (int)length); ferr("ERROR: Region allocation failed, length: %zu\n", length);
return -ENOMEM; return -ENOMEM;
} }
@@ -189,7 +188,7 @@ int rammap(FAR struct file *filep, FAR struct mm_map_entry_s *entry,
* the correct response. * the correct response.
*/ */
ferr("ERROR: Seek to position %d failed\n", (int)entry->offset); ferr("ERROR: Seek to position %zu failed\n", (size_t)entry->offset);
ret = fpos; ret = fpos;
goto errout_with_region; goto errout_with_region;
} }
@@ -209,8 +208,8 @@ int rammap(FAR struct file *filep, FAR struct mm_map_entry_s *entry,
{ {
/* All other read errors are bad. */ /* All other read errors are bad. */
ferr("ERROR: Read failed: offset=%d ret=%d\n", ferr("ERROR: Read failed: offset=%zu ret=%zd\n",
(int)entry->offset, (int)nread); (size_t)entry->offset, nread);
ret = nread; ret = nread;
goto errout_with_region; goto errout_with_region;
@@ -237,7 +236,7 @@ int rammap(FAR struct file *filep, FAR struct mm_map_entry_s *entry,
/* Add the buffer to the list of regions */ /* Add the buffer to the list of regions */
entry->vaddr = rdbuffer; entry->vaddr = rdbuffer;
entry->priv.i = kernel ? 1 : 0; entry->priv.i = kernel;
entry->munmap = unmap_rammap; entry->munmap = unmap_rammap;
ret = mm_map_add(entry); ret = mm_map_add(entry);
+3 -1
View File
@@ -89,6 +89,8 @@
int rammap(FAR struct file *filep, FAR struct mm_map_entry_s *entry, int rammap(FAR struct file *filep, FAR struct mm_map_entry_s *entry,
bool kernel); bool kernel);
#else
# define rammap(file, entry, kernel) (-ENOSYS)
#endif /* CONFIG_FS_RAMMAP */ #endif /* CONFIG_FS_RAMMAP */
#endif /* __FS_MMAP_FS_RAMMAP_H */ #endif /* __FS_MMAP_FS_RAMMAP_H */