blob: 2c4a23113fb5f2e1365ef71414b8cdfabd64fd20 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
David Howellsda4458b2009-02-12 10:40:10 +00002/* NOMMU mmap support for RomFS on MTD devices
3 *
4 * Copyright © 2007 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
David Howellsda4458b2009-02-12 10:40:10 +00006 */
7
8#include <linux/mm.h>
9#include <linux/mtd/super.h>
10#include "internal.h"
11
12/*
13 * try to determine where a shared mapping can be made
14 * - only supported for NOMMU at the moment (MMU can't doesn't copy private
15 * mappings)
16 * - attempts to map through to the underlying MTD device
17 */
18static unsigned long romfs_get_unmapped_area(struct file *file,
19 unsigned long addr,
20 unsigned long len,
21 unsigned long pgoff,
22 unsigned long flags)
23{
24 struct inode *inode = file->f_mapping->host;
25 struct mtd_info *mtd = inode->i_sb->s_mtd;
Bob Liu2b4b2482011-06-27 16:18:06 -070026 unsigned long isize, offset, maxpages, lpages;
Artem Bityutskiy4991e722011-12-28 17:08:03 +020027 int ret;
David Howellsda4458b2009-02-12 10:40:10 +000028
29 if (!mtd)
Artem Bityutskiy4991e722011-12-28 17:08:03 +020030 return (unsigned long) -ENOSYS;
David Howellsda4458b2009-02-12 10:40:10 +000031
Bob Liu2b4b2482011-06-27 16:18:06 -070032 /* the mapping mustn't extend beyond the EOF */
33 lpages = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
David Howellsda4458b2009-02-12 10:40:10 +000034 isize = i_size_read(inode);
35 offset = pgoff << PAGE_SHIFT;
Bob Liu2b4b2482011-06-27 16:18:06 -070036
37 maxpages = (isize + PAGE_SIZE - 1) >> PAGE_SHIFT;
38 if ((pgoff >= maxpages) || (maxpages - pgoff < lpages))
David Howellsda4458b2009-02-12 10:40:10 +000039 return (unsigned long) -EINVAL;
40
Artem Bityutskiy4991e722011-12-28 17:08:03 +020041 if (addr != 0)
42 return (unsigned long) -EINVAL;
David Howellsda4458b2009-02-12 10:40:10 +000043
Artem Bityutskiy4991e722011-12-28 17:08:03 +020044 if (len > mtd->size || pgoff >= (mtd->size >> PAGE_SHIFT))
45 return (unsigned long) -EINVAL;
David Howellsda4458b2009-02-12 10:40:10 +000046
Artem Bityutskiy4991e722011-12-28 17:08:03 +020047 offset += ROMFS_I(inode)->i_dataoffset;
Greg Ungerere4ba4fc2013-04-02 14:25:33 +100048 if (offset >= mtd->size)
Artem Bityutskiy4991e722011-12-28 17:08:03 +020049 return (unsigned long) -EINVAL;
Greg Ungerere4ba4fc2013-04-02 14:25:33 +100050 /* the mapping mustn't extend beyond the EOF */
51 if ((offset + len) > mtd->size)
52 len = mtd->size - offset;
David Howellsda4458b2009-02-12 10:40:10 +000053
Artem Bityutskiy4991e722011-12-28 17:08:03 +020054 ret = mtd_get_unmapped_area(mtd, len, offset, flags);
55 if (ret == -EOPNOTSUPP)
56 ret = -ENOSYS;
57 return (unsigned long) ret;
David Howellsda4458b2009-02-12 10:40:10 +000058}
59
60/*
61 * permit a R/O mapping to be made directly through onto an MTD device if
62 * possible
63 */
64static int romfs_mmap(struct file *file, struct vm_area_struct *vma)
65{
66 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -ENOSYS;
67}
68
Christoph Hellwigb4caecd2015-01-14 10:42:32 +010069static unsigned romfs_mmap_capabilities(struct file *file)
70{
71 struct mtd_info *mtd = file_inode(file)->i_sb->s_mtd;
72
73 if (!mtd)
74 return NOMMU_MAP_COPY;
75 return mtd_mmap_capabilities(mtd);
76}
77
David Howellsda4458b2009-02-12 10:40:10 +000078const struct file_operations romfs_ro_fops = {
79 .llseek = generic_file_llseek,
Al Viroaad4f8b2014-04-02 14:33:16 -040080 .read_iter = generic_file_read_iter,
David Howellsda4458b2009-02-12 10:40:10 +000081 .splice_read = generic_file_splice_read,
82 .mmap = romfs_mmap,
83 .get_unmapped_area = romfs_get_unmapped_area,
Christoph Hellwigb4caecd2015-01-14 10:42:32 +010084 .mmap_capabilities = romfs_mmap_capabilities,
David Howellsda4458b2009-02-12 10:40:10 +000085};