file-nommu.c 6.6 KB

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