^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /* NOMMU mmap support for RomFS on MTD devices
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Copyright © 2007 Red Hat, Inc. All Rights Reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Written by David Howells (dhowells@redhat.com)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/mtd/super.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include "internal.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * try to determine where a shared mapping can be made
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * - only supported for NOMMU at the moment (MMU can't doesn't copy private
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) * mappings)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) * - attempts to map through to the underlying MTD device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) static unsigned long romfs_get_unmapped_area(struct file *file,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) unsigned long addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) unsigned long len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) unsigned long pgoff,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) unsigned long flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) struct inode *inode = file->f_mapping->host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) struct mtd_info *mtd = inode->i_sb->s_mtd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) unsigned long isize, offset, maxpages, lpages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) if (!mtd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) return (unsigned long) -ENOSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) /* the mapping mustn't extend beyond the EOF */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) lpages = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) isize = i_size_read(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) offset = pgoff << PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) maxpages = (isize + PAGE_SIZE - 1) >> PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) if ((pgoff >= maxpages) || (maxpages - pgoff < lpages))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) return (unsigned long) -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) if (addr != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) return (unsigned long) -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) if (len > mtd->size || pgoff >= (mtd->size >> PAGE_SHIFT))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) return (unsigned long) -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) offset += ROMFS_I(inode)->i_dataoffset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) if (offset >= mtd->size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) return (unsigned long) -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) /* the mapping mustn't extend beyond the EOF */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) if ((offset + len) > mtd->size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) len = mtd->size - offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) ret = mtd_get_unmapped_area(mtd, len, offset, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) if (ret == -EOPNOTSUPP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) ret = -ENOSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) return (unsigned long) ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) * permit a R/O mapping to be made directly through onto an MTD device if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) * possible
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) static int romfs_mmap(struct file *file, struct vm_area_struct *vma)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -ENOSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) static unsigned romfs_mmap_capabilities(struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) struct mtd_info *mtd = file_inode(file)->i_sb->s_mtd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) if (!mtd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) return NOMMU_MAP_COPY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) return mtd_mmap_capabilities(mtd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) const struct file_operations romfs_ro_fops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) .llseek = generic_file_llseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) .read_iter = generic_file_read_iter,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) .splice_read = generic_file_splice_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) .mmap = romfs_mmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) .get_unmapped_area = romfs_get_unmapped_area,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) .mmap_capabilities = romfs_mmap_capabilities,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) };