fs/mmap: Support the no backing file for anonymous mapping

Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
This commit is contained in:
Xiang Xiao 2023-01-11 00:51:35 +08:00 committed by Petro Karashchenko
parent ab67d6a75b
commit fcd4a421e3
2 changed files with 20 additions and 15 deletions

View File

@ -55,7 +55,7 @@
#ifdef CONFIG_FS_ANONMAP
int map_anonymous(FAR struct mm_map_entry_s *entry, bool kernel);
#else
#define map_anonymous(entry,kernel) (-ENOSYS)
# define map_anonymous(entry, kernel) (-ENOSYS)
#endif /* CONFIG_FS_ANONMAP */
#endif /* __FS_MMAP_FS_ANONMAP_H */

View File

@ -94,6 +94,22 @@ static int file_mmap_(FAR struct file *filep, FAR void *start,
}
#endif /* CONFIG_DEBUG_FEATURES */
/* Check if we are just be asked to allocate memory, i.e., MAP_ANONYMOUS
* set meaning that the memory is not backed up from a file. The file
* descriptor should be -1 (or refer to opened /dev/zero) in this case.
* The file descriptor is ignored in either case.
*/
if ((flags & MAP_ANONYMOUS) != 0)
{
return map_anonymous(&entry, kernel);
}
if (filep == NULL)
{
return -EBADF;
}
if ((filep->f_oflags & O_WROK) == 0 && prot == PROT_WRITE)
{
ferr("ERROR: Unsupported options for read-only file descriptor,"
@ -107,17 +123,6 @@ static int file_mmap_(FAR struct file *filep, FAR void *start,
return -EACCES;
}
/* Check if we are just be asked to allocate memory, i.e., MAP_ANONYMOUS
* set meaning that the memory is not backed up from a file. The file
* descriptor should be -1 (or refer to opened /dev/zero) in this case.
* The file descriptor is ignored in either case.
*/
if ((flags & MAP_ANONYMOUS) != 0)
{
return map_anonymous(&entry, kernel);
}
/* Call driver's mmap to get the base address of the file in 'mapped'
* in memory.
*/
@ -218,7 +223,7 @@ int file_mmap(FAR struct file *filep, FAR void *start, size_t length,
* MAP_NORESERVE - Ignored
* MAP_POPULATE - Ignored
* MAP_NONBLOCK - Ignored
* fd file descriptor of the backing file -- required.
* fd file descriptor of the backing file.
* offset The offset into the file to map
*
* Returned Value:
@ -247,11 +252,11 @@ int file_mmap(FAR struct file *filep, FAR void *start, size_t length,
FAR void *mmap(FAR void *start, size_t length, int prot, int flags,
int fd, off_t offset)
{
FAR struct file *filep;
FAR struct file *filep = NULL;
FAR void *mapped;
int ret;
if (fs_getfilep(fd, &filep) < 0)
if (fd != -1 && fs_getfilep(fd, &filep) < 0)
{
ferr("ERROR: Invalid file descriptor, fd=%d\n", fd);
ret = -EBADF;