2019-05-27 14:55:01 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2009-02-12 18:40:10 +08:00
|
|
|
/* NOMMU mmap support for RomFS on MTD devices
|
|
|
|
*
|
|
|
|
* Copyright © 2007 Red Hat, Inc. All Rights Reserved.
|
|
|
|
* Written by David Howells (dhowells@redhat.com)
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/mm.h>
|
|
|
|
#include <linux/mtd/super.h>
|
|
|
|
#include "internal.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* try to determine where a shared mapping can be made
|
|
|
|
* - only supported for NOMMU at the moment (MMU can't doesn't copy private
|
|
|
|
* mappings)
|
|
|
|
* - attempts to map through to the underlying MTD device
|
|
|
|
*/
|
|
|
|
static unsigned long romfs_get_unmapped_area(struct file *file,
|
|
|
|
unsigned long addr,
|
|
|
|
unsigned long len,
|
|
|
|
unsigned long pgoff,
|
|
|
|
unsigned long flags)
|
|
|
|
{
|
|
|
|
struct inode *inode = file->f_mapping->host;
|
|
|
|
struct mtd_info *mtd = inode->i_sb->s_mtd;
|
2011-06-28 07:18:06 +08:00
|
|
|
unsigned long isize, offset, maxpages, lpages;
|
2011-12-28 23:08:03 +08:00
|
|
|
int ret;
|
2009-02-12 18:40:10 +08:00
|
|
|
|
|
|
|
if (!mtd)
|
2011-12-28 23:08:03 +08:00
|
|
|
return (unsigned long) -ENOSYS;
|
2009-02-12 18:40:10 +08:00
|
|
|
|
2011-06-28 07:18:06 +08:00
|
|
|
/* the mapping mustn't extend beyond the EOF */
|
|
|
|
lpages = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
|
2009-02-12 18:40:10 +08:00
|
|
|
isize = i_size_read(inode);
|
|
|
|
offset = pgoff << PAGE_SHIFT;
|
2011-06-28 07:18:06 +08:00
|
|
|
|
|
|
|
maxpages = (isize + PAGE_SIZE - 1) >> PAGE_SHIFT;
|
|
|
|
if ((pgoff >= maxpages) || (maxpages - pgoff < lpages))
|
2009-02-12 18:40:10 +08:00
|
|
|
return (unsigned long) -EINVAL;
|
|
|
|
|
2011-12-28 23:08:03 +08:00
|
|
|
if (addr != 0)
|
|
|
|
return (unsigned long) -EINVAL;
|
2009-02-12 18:40:10 +08:00
|
|
|
|
2011-12-28 23:08:03 +08:00
|
|
|
if (len > mtd->size || pgoff >= (mtd->size >> PAGE_SHIFT))
|
|
|
|
return (unsigned long) -EINVAL;
|
2009-02-12 18:40:10 +08:00
|
|
|
|
2011-12-28 23:08:03 +08:00
|
|
|
offset += ROMFS_I(inode)->i_dataoffset;
|
2013-04-02 12:25:33 +08:00
|
|
|
if (offset >= mtd->size)
|
2011-12-28 23:08:03 +08:00
|
|
|
return (unsigned long) -EINVAL;
|
2013-04-02 12:25:33 +08:00
|
|
|
/* the mapping mustn't extend beyond the EOF */
|
|
|
|
if ((offset + len) > mtd->size)
|
|
|
|
len = mtd->size - offset;
|
2009-02-12 18:40:10 +08:00
|
|
|
|
2011-12-28 23:08:03 +08:00
|
|
|
ret = mtd_get_unmapped_area(mtd, len, offset, flags);
|
|
|
|
if (ret == -EOPNOTSUPP)
|
|
|
|
ret = -ENOSYS;
|
|
|
|
return (unsigned long) ret;
|
2009-02-12 18:40:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* permit a R/O mapping to be made directly through onto an MTD device if
|
|
|
|
* possible
|
|
|
|
*/
|
|
|
|
static int romfs_mmap(struct file *file, struct vm_area_struct *vma)
|
|
|
|
{
|
2023-01-03 00:08:54 +08:00
|
|
|
return is_nommu_shared_mapping(vma->vm_flags) ? 0 : -ENOSYS;
|
2009-02-12 18:40:10 +08:00
|
|
|
}
|
|
|
|
|
2015-01-14 17:42:32 +08:00
|
|
|
static unsigned romfs_mmap_capabilities(struct file *file)
|
|
|
|
{
|
|
|
|
struct mtd_info *mtd = file_inode(file)->i_sb->s_mtd;
|
|
|
|
|
|
|
|
if (!mtd)
|
|
|
|
return NOMMU_MAP_COPY;
|
|
|
|
return mtd_mmap_capabilities(mtd);
|
|
|
|
}
|
|
|
|
|
2009-02-12 18:40:10 +08:00
|
|
|
const struct file_operations romfs_ro_fops = {
|
|
|
|
.llseek = generic_file_llseek,
|
2014-04-03 02:33:16 +08:00
|
|
|
.read_iter = generic_file_read_iter,
|
2023-05-22 21:50:15 +08:00
|
|
|
.splice_read = filemap_splice_read,
|
2009-02-12 18:40:10 +08:00
|
|
|
.mmap = romfs_mmap,
|
|
|
|
.get_unmapped_area = romfs_get_unmapped_area,
|
2015-01-14 17:42:32 +08:00
|
|
|
.mmap_capabilities = romfs_mmap_capabilities,
|
2009-02-12 18:40:10 +08:00
|
|
|
};
|