file-nommu.c 6.8 KB

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