Commit | Line | Data |
---|---|---|
2874c5fd | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
da4458bd DH |
2 | /* 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) | |
da4458bd DH |
6 | */ |
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 | */ | |
18 | static 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; | |
2b4b2482 | 26 | unsigned long isize, offset, maxpages, lpages; |
4991e725 | 27 | int ret; |
da4458bd DH |
28 | |
29 | if (!mtd) | |
4991e725 | 30 | return (unsigned long) -ENOSYS; |
da4458bd | 31 | |
2b4b2482 BL |
32 | /* the mapping mustn't extend beyond the EOF */ |
33 | lpages = (len + PAGE_SIZE - 1) >> PAGE_SHIFT; | |
da4458bd DH |
34 | isize = i_size_read(inode); |
35 | offset = pgoff << PAGE_SHIFT; | |
2b4b2482 BL |
36 | |
37 | maxpages = (isize + PAGE_SIZE - 1) >> PAGE_SHIFT; | |
38 | if ((pgoff >= maxpages) || (maxpages - pgoff < lpages)) | |
da4458bd DH |
39 | return (unsigned long) -EINVAL; |
40 | ||
4991e725 AB |
41 | if (addr != 0) |
42 | return (unsigned long) -EINVAL; | |
da4458bd | 43 | |
4991e725 AB |
44 | if (len > mtd->size || pgoff >= (mtd->size >> PAGE_SHIFT)) |
45 | return (unsigned long) -EINVAL; | |
da4458bd | 46 | |
4991e725 | 47 | offset += ROMFS_I(inode)->i_dataoffset; |
e4ba4fc2 | 48 | if (offset >= mtd->size) |
4991e725 | 49 | return (unsigned long) -EINVAL; |
e4ba4fc2 GU |
50 | /* the mapping mustn't extend beyond the EOF */ |
51 | if ((offset + len) > mtd->size) | |
52 | len = mtd->size - offset; | |
da4458bd | 53 | |
4991e725 AB |
54 | ret = mtd_get_unmapped_area(mtd, len, offset, flags); |
55 | if (ret == -EOPNOTSUPP) | |
56 | ret = -ENOSYS; | |
57 | return (unsigned long) ret; | |
da4458bd DH |
58 | } |
59 | ||
60 | /* | |
61 | * permit a R/O mapping to be made directly through onto an MTD device if | |
62 | * possible | |
63 | */ | |
64 | static int romfs_mmap(struct file *file, struct vm_area_struct *vma) | |
65 | { | |
fc4f4be9 | 66 | return is_nommu_shared_mapping(vma->vm_flags) ? 0 : -ENOSYS; |
da4458bd DH |
67 | } |
68 | ||
b4caecd4 CH |
69 | static 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 | ||
da4458bd DH |
78 | const struct file_operations romfs_ro_fops = { |
79 | .llseek = generic_file_llseek, | |
aad4f8bb | 80 | .read_iter = generic_file_read_iter, |
2cb1e089 | 81 | .splice_read = filemap_splice_read, |
da4458bd DH |
82 | .mmap = romfs_mmap, |
83 | .get_unmapped_area = romfs_get_unmapped_area, | |
b4caecd4 | 84 | .mmap_capabilities = romfs_mmap_capabilities, |
da4458bd | 85 | }; |