1
2
3
4
5
6
7
8
9
10
11
12#include <linux/mm.h>
13#include <linux/mtd/super.h>
14#include "internal.h"
15
16
17
18
19
20
21
22static unsigned long romfs_get_unmapped_area(struct file *file,
23 unsigned long addr,
24 unsigned long len,
25 unsigned long pgoff,
26 unsigned long flags)
27{
28 struct inode *inode = file->f_mapping->host;
29 struct mtd_info *mtd = inode->i_sb->s_mtd;
30 unsigned long isize, offset, maxpages, lpages;
31 int ret;
32
33 if (!mtd)
34 return (unsigned long) -ENOSYS;
35
36
37 lpages = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
38 isize = i_size_read(inode);
39 offset = pgoff << PAGE_SHIFT;
40
41 maxpages = (isize + PAGE_SIZE - 1) >> PAGE_SHIFT;
42 if ((pgoff >= maxpages) || (maxpages - pgoff < lpages))
43 return (unsigned long) -EINVAL;
44
45 if (addr != 0)
46 return (unsigned long) -EINVAL;
47
48 if (len > mtd->size || pgoff >= (mtd->size >> PAGE_SHIFT))
49 return (unsigned long) -EINVAL;
50
51 offset += ROMFS_I(inode)->i_dataoffset;
52 if (offset >= mtd->size)
53 return (unsigned long) -EINVAL;
54
55 if ((offset + len) > mtd->size)
56 len = mtd->size - offset;
57
58 ret = mtd_get_unmapped_area(mtd, len, offset, flags);
59 if (ret == -EOPNOTSUPP)
60 ret = -ENOSYS;
61 return (unsigned long) ret;
62}
63
64
65
66
67
68static int romfs_mmap(struct file *file, struct vm_area_struct *vma)
69{
70 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -ENOSYS;
71}
72
73static unsigned romfs_mmap_capabilities(struct file *file)
74{
75 struct mtd_info *mtd = file_inode(file)->i_sb->s_mtd;
76
77 if (!mtd)
78 return NOMMU_MAP_COPY;
79 return mtd_mmap_capabilities(mtd);
80}
81
82const struct file_operations romfs_ro_fops = {
83 .llseek = generic_file_llseek,
84 .read_iter = generic_file_read_iter,
85 .splice_read = generic_file_splice_read,
86 .mmap = romfs_mmap,
87 .get_unmapped_area = romfs_get_unmapped_area,
88 .mmap_capabilities = romfs_mmap_capabilities,
89};
90