file-nommu.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /* file-nommu.c: no-MMU version of ramfs
  2. *
  3. * Copyright (C) 2005 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/module.h>
  12. #include <linux/fs.h>
  13. #include <linux/mm.h>
  14. #include <linux/pagemap.h>
  15. #include <linux/highmem.h>
  16. #include <linux/init.h>
  17. #include <linux/string.h>
  18. #include <linux/backing-dev.h>
  19. #include <linux/ramfs.h>
  20. #include <linux/pagevec.h>
  21. #include <linux/mman.h>
  22. #include <linux/sched.h>
  23. #include <linux/slab.h>
  24. #include <asm/uaccess.h>
  25. #include "internal.h"
  26. static int ramfs_nommu_setattr(struct dentry *, struct iattr *);
  27. static unsigned long ramfs_nommu_get_unmapped_area(struct file *file,
  28. unsigned long addr,
  29. unsigned long len,
  30. unsigned long pgoff,
  31. unsigned long flags);
  32. static int ramfs_nommu_mmap(struct file *file, struct vm_area_struct *vma);
  33. static unsigned ramfs_mmap_capabilities(struct file *file)
  34. {
  35. return NOMMU_MAP_DIRECT | NOMMU_MAP_COPY | NOMMU_MAP_READ |
  36. NOMMU_MAP_WRITE | NOMMU_MAP_EXEC;
  37. }
  38. const struct file_operations ramfs_file_operations = {
  39. .mmap_capabilities = ramfs_mmap_capabilities,
  40. .mmap = ramfs_nommu_mmap,
  41. .get_unmapped_area = ramfs_nommu_get_unmapped_area,
  42. .read = new_sync_read,
  43. .read_iter = generic_file_read_iter,
  44. .write = new_sync_write,
  45. .write_iter = generic_file_write_iter,
  46. .fsync = noop_fsync,
  47. .splice_read = generic_file_splice_read,
  48. .splice_write = iter_file_splice_write,
  49. .llseek = generic_file_llseek,
  50. };
  51. const struct inode_operations ramfs_file_inode_operations = {
  52. .setattr = ramfs_nommu_setattr,
  53. .getattr = simple_getattr,
  54. };
  55. /*****************************************************************************/
  56. /*
  57. * add a contiguous set of pages into a ramfs inode when it's truncated from
  58. * size 0 on the assumption that it's going to be used for an mmap of shared
  59. * memory
  60. */
  61. int ramfs_nommu_expand_for_mapping(struct inode *inode, size_t newsize)
  62. {
  63. unsigned long npages, xpages, loop;
  64. struct page *pages;
  65. unsigned order;
  66. void *data;
  67. int ret;
  68. /* make various checks */
  69. order = get_order(newsize);
  70. if (unlikely(order >= MAX_ORDER))
  71. return -EFBIG;
  72. ret = inode_newsize_ok(inode, newsize);
  73. if (ret)
  74. return ret;
  75. i_size_write(inode, newsize);
  76. /* allocate enough contiguous pages to be able to satisfy the
  77. * request */
  78. pages = alloc_pages(mapping_gfp_mask(inode->i_mapping), order);
  79. if (!pages)
  80. return -ENOMEM;
  81. /* split the high-order page into an array of single pages */
  82. xpages = 1UL << order;
  83. npages = (newsize + PAGE_SIZE - 1) >> PAGE_SHIFT;
  84. split_page(pages, order);
  85. /* trim off any pages we don't actually require */
  86. for (loop = npages; loop < xpages; loop++)
  87. __free_page(pages + loop);
  88. /* clear the memory we allocated */
  89. newsize = PAGE_SIZE * npages;
  90. data = page_address(pages);
  91. memset(data, 0, newsize);
  92. /* attach all the pages to the inode's address space */
  93. for (loop = 0; loop < npages; loop++) {
  94. struct page *page = pages + loop;
  95. ret = add_to_page_cache_lru(page, inode->i_mapping, loop,
  96. GFP_KERNEL);
  97. if (ret < 0)
  98. goto add_error;
  99. /* prevent the page from being discarded on memory pressure */
  100. SetPageDirty(page);
  101. SetPageUptodate(page);
  102. unlock_page(page);
  103. put_page(page);
  104. }
  105. return 0;
  106. add_error:
  107. while (loop < npages)
  108. __free_page(pages + loop++);
  109. return ret;
  110. }
  111. /*****************************************************************************/
  112. /*
  113. *
  114. */
  115. static int ramfs_nommu_resize(struct inode *inode, loff_t newsize, loff_t size)
  116. {
  117. int ret;
  118. /* assume a truncate from zero size is going to be for the purposes of
  119. * shared mmap */
  120. if (size == 0) {
  121. if (unlikely(newsize >> 32))
  122. return -EFBIG;
  123. return ramfs_nommu_expand_for_mapping(inode, newsize);
  124. }
  125. /* check that a decrease in size doesn't cut off any shared mappings */
  126. if (newsize < size) {
  127. ret = nommu_shrink_inode_mappings(inode, size, newsize);
  128. if (ret < 0)
  129. return ret;
  130. }
  131. truncate_setsize(inode, newsize);
  132. return 0;
  133. }
  134. /*****************************************************************************/
  135. /*
  136. * handle a change of attributes
  137. * - we're specifically interested in a change of size
  138. */
  139. static int ramfs_nommu_setattr(struct dentry *dentry, struct iattr *ia)
  140. {
  141. struct inode *inode = dentry->d_inode;
  142. unsigned int old_ia_valid = ia->ia_valid;
  143. int ret = 0;
  144. /* POSIX UID/GID verification for setting inode attributes */
  145. ret = inode_change_ok(inode, ia);
  146. if (ret)
  147. return ret;
  148. /* pick out size-changing events */
  149. if (ia->ia_valid & ATTR_SIZE) {
  150. loff_t size = inode->i_size;
  151. if (ia->ia_size != size) {
  152. ret = ramfs_nommu_resize(inode, ia->ia_size, size);
  153. if (ret < 0 || ia->ia_valid == ATTR_SIZE)
  154. goto out;
  155. } else {
  156. /* we skipped the truncate but must still update
  157. * timestamps
  158. */
  159. ia->ia_valid |= ATTR_MTIME|ATTR_CTIME;
  160. }
  161. }
  162. setattr_copy(inode, ia);
  163. out:
  164. ia->ia_valid = old_ia_valid;
  165. return ret;
  166. }
  167. /*****************************************************************************/
  168. /*
  169. * try to determine where a shared mapping can be made
  170. * - we require that:
  171. * - the pages to be mapped must exist
  172. * - the pages be physically contiguous in sequence
  173. */
  174. static unsigned long ramfs_nommu_get_unmapped_area(struct file *file,
  175. unsigned long addr, unsigned long len,
  176. unsigned long pgoff, unsigned long flags)
  177. {
  178. unsigned long maxpages, lpages, nr, loop, ret;
  179. struct inode *inode = file_inode(file);
  180. struct page **pages = NULL, **ptr, *page;
  181. loff_t isize;
  182. if (!(flags & MAP_SHARED))
  183. return addr;
  184. /* the mapping mustn't extend beyond the EOF */
  185. lpages = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
  186. isize = i_size_read(inode);
  187. ret = -EINVAL;
  188. maxpages = (isize + PAGE_SIZE - 1) >> PAGE_SHIFT;
  189. if (pgoff >= maxpages)
  190. goto out;
  191. if (maxpages - pgoff < lpages)
  192. goto out;
  193. /* gang-find the pages */
  194. ret = -ENOMEM;
  195. pages = kcalloc(lpages, sizeof(struct page *), GFP_KERNEL);
  196. if (!pages)
  197. goto out_free;
  198. nr = find_get_pages(inode->i_mapping, pgoff, lpages, pages);
  199. if (nr != lpages)
  200. goto out_free_pages; /* leave if some pages were missing */
  201. /* check the pages for physical adjacency */
  202. ptr = pages;
  203. page = *ptr++;
  204. page++;
  205. for (loop = lpages; loop > 1; loop--)
  206. if (*ptr++ != page++)
  207. goto out_free_pages;
  208. /* okay - all conditions fulfilled */
  209. ret = (unsigned long) page_address(pages[0]);
  210. out_free_pages:
  211. ptr = pages;
  212. for (loop = nr; loop > 0; loop--)
  213. put_page(*ptr++);
  214. out_free:
  215. kfree(pages);
  216. out:
  217. return ret;
  218. }
  219. /*****************************************************************************/
  220. /*
  221. * set up a mapping for shared memory segments
  222. */
  223. static int ramfs_nommu_mmap(struct file *file, struct vm_area_struct *vma)
  224. {
  225. if (!(vma->vm_flags & VM_SHARED))
  226. return -ENOSYS;
  227. file_accessed(file);
  228. vma->vm_ops = &generic_file_vm_ops;
  229. return 0;
  230. }