mmap-nommu.c 2.3 KB

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