David Howells | da4458b | 2009-02-12 10:40:10 +0000 | [diff] [blame] | 1 | /* NOMMU mmap support for RomFS on MTD devices |
| 2 | * |
| 3 | * Copyright © 2007 Red Hat, Inc. All Rights Reserved. |
| 4 | * Written by David Howells (dhowells@redhat.com) |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | */ |
| 11 | |
| 12 | #include <linux/mm.h> |
| 13 | #include <linux/mtd/super.h> |
| 14 | #include "internal.h" |
| 15 | |
| 16 | /* |
| 17 | * try to determine where a shared mapping can be made |
| 18 | * - only supported for NOMMU at the moment (MMU can't doesn't copy private |
| 19 | * mappings) |
| 20 | * - attempts to map through to the underlying MTD device |
| 21 | */ |
| 22 | static 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; |
Bob Liu | 2b4b248 | 2011-06-27 16:18:06 -0700 | [diff] [blame] | 30 | unsigned long isize, offset, maxpages, lpages; |
Artem Bityutskiy | 4991e72 | 2011-12-28 17:08:03 +0200 | [diff] [blame] | 31 | int ret; |
David Howells | da4458b | 2009-02-12 10:40:10 +0000 | [diff] [blame] | 32 | |
| 33 | if (!mtd) |
Artem Bityutskiy | 4991e72 | 2011-12-28 17:08:03 +0200 | [diff] [blame] | 34 | return (unsigned long) -ENOSYS; |
David Howells | da4458b | 2009-02-12 10:40:10 +0000 | [diff] [blame] | 35 | |
Bob Liu | 2b4b248 | 2011-06-27 16:18:06 -0700 | [diff] [blame] | 36 | /* the mapping mustn't extend beyond the EOF */ |
| 37 | lpages = (len + PAGE_SIZE - 1) >> PAGE_SHIFT; |
David Howells | da4458b | 2009-02-12 10:40:10 +0000 | [diff] [blame] | 38 | isize = i_size_read(inode); |
| 39 | offset = pgoff << PAGE_SHIFT; |
Bob Liu | 2b4b248 | 2011-06-27 16:18:06 -0700 | [diff] [blame] | 40 | |
| 41 | maxpages = (isize + PAGE_SIZE - 1) >> PAGE_SHIFT; |
| 42 | if ((pgoff >= maxpages) || (maxpages - pgoff < lpages)) |
David Howells | da4458b | 2009-02-12 10:40:10 +0000 | [diff] [blame] | 43 | return (unsigned long) -EINVAL; |
| 44 | |
Artem Bityutskiy | 4991e72 | 2011-12-28 17:08:03 +0200 | [diff] [blame] | 45 | if (addr != 0) |
| 46 | return (unsigned long) -EINVAL; |
David Howells | da4458b | 2009-02-12 10:40:10 +0000 | [diff] [blame] | 47 | |
Artem Bityutskiy | 4991e72 | 2011-12-28 17:08:03 +0200 | [diff] [blame] | 48 | if (len > mtd->size || pgoff >= (mtd->size >> PAGE_SHIFT)) |
| 49 | return (unsigned long) -EINVAL; |
David Howells | da4458b | 2009-02-12 10:40:10 +0000 | [diff] [blame] | 50 | |
Artem Bityutskiy | 4991e72 | 2011-12-28 17:08:03 +0200 | [diff] [blame] | 51 | offset += ROMFS_I(inode)->i_dataoffset; |
Greg Ungerer | e4ba4fc | 2013-04-02 14:25:33 +1000 | [diff] [blame] | 52 | if (offset >= mtd->size) |
Artem Bityutskiy | 4991e72 | 2011-12-28 17:08:03 +0200 | [diff] [blame] | 53 | return (unsigned long) -EINVAL; |
Greg Ungerer | e4ba4fc | 2013-04-02 14:25:33 +1000 | [diff] [blame] | 54 | /* the mapping mustn't extend beyond the EOF */ |
| 55 | if ((offset + len) > mtd->size) |
| 56 | len = mtd->size - offset; |
David Howells | da4458b | 2009-02-12 10:40:10 +0000 | [diff] [blame] | 57 | |
Artem Bityutskiy | 4991e72 | 2011-12-28 17:08:03 +0200 | [diff] [blame] | 58 | ret = mtd_get_unmapped_area(mtd, len, offset, flags); |
| 59 | if (ret == -EOPNOTSUPP) |
| 60 | ret = -ENOSYS; |
| 61 | return (unsigned long) ret; |
David Howells | da4458b | 2009-02-12 10:40:10 +0000 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | /* |
| 65 | * permit a R/O mapping to be made directly through onto an MTD device if |
| 66 | * possible |
| 67 | */ |
| 68 | static 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 | |
Christoph Hellwig | b4caecd | 2015-01-14 10:42:32 +0100 | [diff] [blame] | 73 | static 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 | |
David Howells | da4458b | 2009-02-12 10:40:10 +0000 | [diff] [blame] | 82 | const struct file_operations romfs_ro_fops = { |
| 83 | .llseek = generic_file_llseek, |
Al Viro | aad4f8b | 2014-04-02 14:33:16 -0400 | [diff] [blame] | 84 | .read = new_sync_read, |
| 85 | .read_iter = generic_file_read_iter, |
David Howells | da4458b | 2009-02-12 10:40:10 +0000 | [diff] [blame] | 86 | .splice_read = generic_file_splice_read, |
| 87 | .mmap = romfs_mmap, |
| 88 | .get_unmapped_area = romfs_get_unmapped_area, |
Christoph Hellwig | b4caecd | 2015-01-14 10:42:32 +0100 | [diff] [blame] | 89 | .mmap_capabilities = romfs_mmap_capabilities, |
David Howells | da4458b | 2009-02-12 10:40:10 +0000 | [diff] [blame] | 90 | }; |