mmap-nommu.c 2.3 KB

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