VisionFive2 Linux kernel

StarFive Tech Linux Kernel for VisionFive (JH7110) boards (mirror)

More than 9999 Commits   32 Branches   54 Tags
2874c5fd28426 (Thomas Gleixner   2019-05-27 08:55:01 +0200  1) // SPDX-License-Identifier: GPL-2.0-or-later
da4458bda237a (David Howells     2009-02-12 10:40:10 +0000  2) /* NOMMU mmap support for RomFS on MTD devices
da4458bda237a (David Howells     2009-02-12 10:40:10 +0000  3)  *
da4458bda237a (David Howells     2009-02-12 10:40:10 +0000  4)  * Copyright © 2007 Red Hat, Inc. All Rights Reserved.
da4458bda237a (David Howells     2009-02-12 10:40:10 +0000  5)  * Written by David Howells (dhowells@redhat.com)
da4458bda237a (David Howells     2009-02-12 10:40:10 +0000  6)  */
da4458bda237a (David Howells     2009-02-12 10:40:10 +0000  7) 
da4458bda237a (David Howells     2009-02-12 10:40:10 +0000  8) #include <linux/mm.h>
da4458bda237a (David Howells     2009-02-12 10:40:10 +0000  9) #include <linux/mtd/super.h>
da4458bda237a (David Howells     2009-02-12 10:40:10 +0000 10) #include "internal.h"
da4458bda237a (David Howells     2009-02-12 10:40:10 +0000 11) 
da4458bda237a (David Howells     2009-02-12 10:40:10 +0000 12) /*
da4458bda237a (David Howells     2009-02-12 10:40:10 +0000 13)  * try to determine where a shared mapping can be made
da4458bda237a (David Howells     2009-02-12 10:40:10 +0000 14)  * - only supported for NOMMU at the moment (MMU can't doesn't copy private
da4458bda237a (David Howells     2009-02-12 10:40:10 +0000 15)  *   mappings)
da4458bda237a (David Howells     2009-02-12 10:40:10 +0000 16)  * - attempts to map through to the underlying MTD device
da4458bda237a (David Howells     2009-02-12 10:40:10 +0000 17)  */
da4458bda237a (David Howells     2009-02-12 10:40:10 +0000 18) static unsigned long romfs_get_unmapped_area(struct file *file,
da4458bda237a (David Howells     2009-02-12 10:40:10 +0000 19) 					     unsigned long addr,
da4458bda237a (David Howells     2009-02-12 10:40:10 +0000 20) 					     unsigned long len,
da4458bda237a (David Howells     2009-02-12 10:40:10 +0000 21) 					     unsigned long pgoff,
da4458bda237a (David Howells     2009-02-12 10:40:10 +0000 22) 					     unsigned long flags)
da4458bda237a (David Howells     2009-02-12 10:40:10 +0000 23) {
da4458bda237a (David Howells     2009-02-12 10:40:10 +0000 24) 	struct inode *inode = file->f_mapping->host;
da4458bda237a (David Howells     2009-02-12 10:40:10 +0000 25) 	struct mtd_info *mtd = inode->i_sb->s_mtd;
2b4b2482e70eb (Bob Liu           2011-06-27 16:18:06 -0700 26) 	unsigned long isize, offset, maxpages, lpages;
4991e7251ed95 (Artem Bityutskiy  2011-12-28 17:08:03 +0200 27) 	int ret;
da4458bda237a (David Howells     2009-02-12 10:40:10 +0000 28) 
da4458bda237a (David Howells     2009-02-12 10:40:10 +0000 29) 	if (!mtd)
4991e7251ed95 (Artem Bityutskiy  2011-12-28 17:08:03 +0200 30) 		return (unsigned long) -ENOSYS;
da4458bda237a (David Howells     2009-02-12 10:40:10 +0000 31) 
2b4b2482e70eb (Bob Liu           2011-06-27 16:18:06 -0700 32) 	/* the mapping mustn't extend beyond the EOF */
2b4b2482e70eb (Bob Liu           2011-06-27 16:18:06 -0700 33) 	lpages = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
da4458bda237a (David Howells     2009-02-12 10:40:10 +0000 34) 	isize = i_size_read(inode);
da4458bda237a (David Howells     2009-02-12 10:40:10 +0000 35) 	offset = pgoff << PAGE_SHIFT;
2b4b2482e70eb (Bob Liu           2011-06-27 16:18:06 -0700 36) 
2b4b2482e70eb (Bob Liu           2011-06-27 16:18:06 -0700 37) 	maxpages = (isize + PAGE_SIZE - 1) >> PAGE_SHIFT;
2b4b2482e70eb (Bob Liu           2011-06-27 16:18:06 -0700 38) 	if ((pgoff >= maxpages) || (maxpages - pgoff < lpages))
da4458bda237a (David Howells     2009-02-12 10:40:10 +0000 39) 		return (unsigned long) -EINVAL;
da4458bda237a (David Howells     2009-02-12 10:40:10 +0000 40) 
4991e7251ed95 (Artem Bityutskiy  2011-12-28 17:08:03 +0200 41) 	if (addr != 0)
4991e7251ed95 (Artem Bityutskiy  2011-12-28 17:08:03 +0200 42) 		return (unsigned long) -EINVAL;
da4458bda237a (David Howells     2009-02-12 10:40:10 +0000 43) 
4991e7251ed95 (Artem Bityutskiy  2011-12-28 17:08:03 +0200 44) 	if (len > mtd->size || pgoff >= (mtd->size >> PAGE_SHIFT))
4991e7251ed95 (Artem Bityutskiy  2011-12-28 17:08:03 +0200 45) 		return (unsigned long) -EINVAL;
da4458bda237a (David Howells     2009-02-12 10:40:10 +0000 46) 
4991e7251ed95 (Artem Bityutskiy  2011-12-28 17:08:03 +0200 47) 	offset += ROMFS_I(inode)->i_dataoffset;
e4ba4fc2b98f6 (Greg Ungerer      2013-04-02 14:25:33 +1000 48) 	if (offset >= mtd->size)
4991e7251ed95 (Artem Bityutskiy  2011-12-28 17:08:03 +0200 49) 		return (unsigned long) -EINVAL;
e4ba4fc2b98f6 (Greg Ungerer      2013-04-02 14:25:33 +1000 50) 	/* the mapping mustn't extend beyond the EOF */
e4ba4fc2b98f6 (Greg Ungerer      2013-04-02 14:25:33 +1000 51) 	if ((offset + len) > mtd->size)
e4ba4fc2b98f6 (Greg Ungerer      2013-04-02 14:25:33 +1000 52) 		len = mtd->size - offset;
da4458bda237a (David Howells     2009-02-12 10:40:10 +0000 53) 
4991e7251ed95 (Artem Bityutskiy  2011-12-28 17:08:03 +0200 54) 	ret = mtd_get_unmapped_area(mtd, len, offset, flags);
4991e7251ed95 (Artem Bityutskiy  2011-12-28 17:08:03 +0200 55) 	if (ret == -EOPNOTSUPP)
4991e7251ed95 (Artem Bityutskiy  2011-12-28 17:08:03 +0200 56) 		ret = -ENOSYS;
4991e7251ed95 (Artem Bityutskiy  2011-12-28 17:08:03 +0200 57) 	return (unsigned long) ret;
da4458bda237a (David Howells     2009-02-12 10:40:10 +0000 58) }
da4458bda237a (David Howells     2009-02-12 10:40:10 +0000 59) 
da4458bda237a (David Howells     2009-02-12 10:40:10 +0000 60) /*
da4458bda237a (David Howells     2009-02-12 10:40:10 +0000 61)  * permit a R/O mapping to be made directly through onto an MTD device if
da4458bda237a (David Howells     2009-02-12 10:40:10 +0000 62)  * possible
da4458bda237a (David Howells     2009-02-12 10:40:10 +0000 63)  */
da4458bda237a (David Howells     2009-02-12 10:40:10 +0000 64) static int romfs_mmap(struct file *file, struct vm_area_struct *vma)
da4458bda237a (David Howells     2009-02-12 10:40:10 +0000 65) {
da4458bda237a (David Howells     2009-02-12 10:40:10 +0000 66) 	return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -ENOSYS;
da4458bda237a (David Howells     2009-02-12 10:40:10 +0000 67) }
da4458bda237a (David Howells     2009-02-12 10:40:10 +0000 68) 
b4caecd48005f (Christoph Hellwig 2015-01-14 10:42:32 +0100 69) static unsigned romfs_mmap_capabilities(struct file *file)
b4caecd48005f (Christoph Hellwig 2015-01-14 10:42:32 +0100 70) {
b4caecd48005f (Christoph Hellwig 2015-01-14 10:42:32 +0100 71) 	struct mtd_info *mtd = file_inode(file)->i_sb->s_mtd;
b4caecd48005f (Christoph Hellwig 2015-01-14 10:42:32 +0100 72) 
b4caecd48005f (Christoph Hellwig 2015-01-14 10:42:32 +0100 73) 	if (!mtd)
b4caecd48005f (Christoph Hellwig 2015-01-14 10:42:32 +0100 74) 		return NOMMU_MAP_COPY;
b4caecd48005f (Christoph Hellwig 2015-01-14 10:42:32 +0100 75) 	return mtd_mmap_capabilities(mtd);
b4caecd48005f (Christoph Hellwig 2015-01-14 10:42:32 +0100 76) }
b4caecd48005f (Christoph Hellwig 2015-01-14 10:42:32 +0100 77) 
da4458bda237a (David Howells     2009-02-12 10:40:10 +0000 78) const struct file_operations romfs_ro_fops = {
da4458bda237a (David Howells     2009-02-12 10:40:10 +0000 79) 	.llseek			= generic_file_llseek,
aad4f8bb42af0 (Al Viro           2014-04-02 14:33:16 -0400 80) 	.read_iter		= generic_file_read_iter,
da4458bda237a (David Howells     2009-02-12 10:40:10 +0000 81) 	.splice_read		= generic_file_splice_read,
da4458bda237a (David Howells     2009-02-12 10:40:10 +0000 82) 	.mmap			= romfs_mmap,
da4458bda237a (David Howells     2009-02-12 10:40:10 +0000 83) 	.get_unmapped_area	= romfs_get_unmapped_area,
b4caecd48005f (Christoph Hellwig 2015-01-14 10:42:32 +0100 84) 	.mmap_capabilities	= romfs_mmap_capabilities,
da4458bda237a (David Howells     2009-02-12 10:40:10 +0000 85) };