blob: e1a7779dd3cb18e72276569f8d2b0ce77e53a42c [file] [log] [blame]
David Howellsda4458b2009-02-12 10:40:10 +00001/* 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 */
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;
Bob Liu2b4b2482011-06-27 16:18:06 -070030 unsigned long isize, offset, maxpages, lpages;
Artem Bityutskiy4991e722011-12-28 17:08:03 +020031 int ret;
David Howellsda4458b2009-02-12 10:40:10 +000032
33 if (!mtd)
Artem Bityutskiy4991e722011-12-28 17:08:03 +020034 return (unsigned long) -ENOSYS;
David Howellsda4458b2009-02-12 10:40:10 +000035
Bob Liu2b4b2482011-06-27 16:18:06 -070036 /* the mapping mustn't extend beyond the EOF */
37 lpages = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
David Howellsda4458b2009-02-12 10:40:10 +000038 isize = i_size_read(inode);
39 offset = pgoff << PAGE_SHIFT;
Bob Liu2b4b2482011-06-27 16:18:06 -070040
41 maxpages = (isize + PAGE_SIZE - 1) >> PAGE_SHIFT;
42 if ((pgoff >= maxpages) || (maxpages - pgoff < lpages))
David Howellsda4458b2009-02-12 10:40:10 +000043 return (unsigned long) -EINVAL;
44
Artem Bityutskiy4991e722011-12-28 17:08:03 +020045 if (addr != 0)
46 return (unsigned long) -EINVAL;
David Howellsda4458b2009-02-12 10:40:10 +000047
Artem Bityutskiy4991e722011-12-28 17:08:03 +020048 if (len > mtd->size || pgoff >= (mtd->size >> PAGE_SHIFT))
49 return (unsigned long) -EINVAL;
David Howellsda4458b2009-02-12 10:40:10 +000050
Artem Bityutskiy4991e722011-12-28 17:08:03 +020051 offset += ROMFS_I(inode)->i_dataoffset;
52 if (offset > mtd->size - len)
53 return (unsigned long) -EINVAL;
David Howellsda4458b2009-02-12 10:40:10 +000054
Artem Bityutskiy4991e722011-12-28 17:08:03 +020055 ret = mtd_get_unmapped_area(mtd, len, offset, flags);
56 if (ret == -EOPNOTSUPP)
57 ret = -ENOSYS;
58 return (unsigned long) ret;
David Howellsda4458b2009-02-12 10:40:10 +000059}
60
61/*
62 * permit a R/O mapping to be made directly through onto an MTD device if
63 * possible
64 */
65static int romfs_mmap(struct file *file, struct vm_area_struct *vma)
66{
67 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -ENOSYS;
68}
69
70const struct file_operations romfs_ro_fops = {
71 .llseek = generic_file_llseek,
72 .read = do_sync_read,
73 .aio_read = generic_file_aio_read,
74 .splice_read = generic_file_splice_read,
75 .mmap = romfs_mmap,
76 .get_unmapped_area = romfs_get_unmapped_area,
77};